By click event on the document it may register faster, but every time it is clicked it will run the function. With the click on the A only when that is clicked will the action happen. I find this to be smoother. If your webpage has a form, there is a lot of clicking. Also rich interfaces have lots of clicks that may not be tied to A elements anyway.
It isn't much different than the original, but I haven't had much luck getting .html.href to work in the past. On Jan 23, 11:26 am, MorningZ <morni...@gmail.com> wrote: > or how it's "slower".... say there's 100 <a> on the page (which > wouldn't be too uncommon for say, a blog site), that's 100 event's > wired up sitting in ready to "go"... using event delegation, one > single event is wired up and if a user clicks somewhere (and really, > how much clicking does a web page actually get?), it does a single if > statement asking "was this a link?" > > On Jan 23, 10:52 am, Amos King <amos.l.k...@gmail.com> wrote: > > > > > Greg, I'm not sure how your's is much diffrent then the original. > > > On Sat, Jan 23, 2010 at 7:04 AM, Greg Tarnoff <greg.tarn...@gmail.com>wrote: > > > > 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 <k3liu...@gmail.com> 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+40758833 281 > > > > > Puncthttp://designpunct.ro > > > -- > > Amos Kinghttp://dirtyInformation.comhttp://github.com/Adkron > > -- > > Looking for something to do? Visithttp://ImThere.com