Actually all of these are slow. The last example will run anytime you
click the page. You only want to run this if they click an A element.
So attach a click event to A. Try this:
$(document).ready(function(){
$('a').click(function(){
alert($(this).attr('href'););
});
});
only do the e.preventDefault(); if you don't want it to go somewhere.
I use this method all the time to get the HREF, always putting it into
a variable, and affect the page. Usually for things like sliding
panels. By putting in #myid it remains accessible when JS isnturned
off.
On Jan 23, 4:30 am, Andrei Eftimie <[email protected]> wrote:
> >> $(document).ready(function() {
> >> $("a").click(function(event) {
> >> alert( "You clicked a link to " + this.href );
> >> return false;
> >> });
> >> });
>
> This method is very slow.
>
> Try using event delegation:
>
> $(document).ready(function() {
> $(document).click(function(event){
> if ($(event.target).is('a') {
> alert( "You clicked a link to " + event.target.href);
> return false;
> }
> });
>
> });
>
> --
> Andrei Eftimiehttp://eftimie.com+40 758 833 281
>
> Puncthttp://designpunct.ro