jQuery .live event propagation
I have a little problem with jQuery .live
method. I am using it for catching ajax events for Google Analytics on my website, but in case I have a link with an inner image, the click event is fired up from the image and my live binded click event does not catch it.
I really dont like to add these even开发者_如何学JAVAts manually everytime after changing content and I dont like to bind it to the image (because of the missing href parameter, this case I had use some .parent method), so what is the best way how to handle this?
Notice: I am not sure about efficiency of the .live method, so in case there are big performance differences, please tell me that:) I tried to profile it in webkit profiler, but I didn't see any difference..
Just place a click(function(event) { ... })
handler on the static parent element, and find the element which started the event with event.target
.
Assuming you have an a
containing an img
, any event on the img
should bubble to the a
, which will catch it.
You could also try using the .delegate() method (http://api.jquery.com/delegate/)
Here is some more info regarding .live() vs. delegate():
- http://brandonaaron.net/blog/2010/03/4/event-delegation-with-jquery
- http://www.youtube.com/watch?v=23Q4S9hmHQY
- jQuery: live() vs delegate() [stackoverflow.com]
Update:
Here is a post by Jupiter 24 about "Why you should never use jQuery live":
- http://jupiterjs.com/news/why-you-should-never-use-jquery-live
精彩评论