开发者

jQuery: is there a reason not to always use live() for click and other functions?

开发者 https://www.devze.com 2023-04-08 04:46 出处:网络
I really like using .li开发者_开发知识库ve() in jQuery for click() focus() blur() and other interaction events.

I really like using .li开发者_开发知识库ve() in jQuery for click() focus() blur() and other interaction events.

I do a lot of prototyping, so I find it gives me great flexibility if I want to dynamically add elements. For that reason, I find myself drawn to the idea of using it by default all the time. Is this a good idea, or is this bad performance?

Does using .live('click',function(){}) slow things down in a way that .click(function(){}) doesn't?


If you don't need your events to bubble up to the top of the DOM and you know the context in which your event will occur then delegate() is a much better choice in terms of performance. See this stackoverflow post on why delegate() is better than live() in this regards.


I think following answer will be suitable for the performance impact it does create How does jQuery .live() work?


Regarding the performance, using the live is better in most cases. However live has several pitfalls, which are described in the documentation here http://api.jquery.com/live/#caveats.


I remember that .live uses event bubbling.

In my experience, I've seen noticeable performance hit using .live in big document with a frequently triggered event like mouseover.

jQuery Doc:

But as of jQuery 1.4, event bubbling can optionally stop at a DOM element "context".

So, you can use that to minimize the performance effect.

0

精彩评论

暂无评论...
验证码 换一张
取 消