livequery performance

本秂侑毒 提交于 2019-11-30 23:04:10

问题


I've recently discovered that livequery plugin for jQuery may be quite wasteful, as it does not use event delegation but binds all bindable events and re-checks the whole DOM on each change

if anyone has more information or suggestions on best practices using livequery and .live(), I would be very grateful


回答1:


It is rare that you would actually need a plugin like livequery. Probably the only time you really need it is if you need to react to changes to the DOM made by some other jQuery code that you can not modify.

While .live() does use event delegation, it does it on the document level, which means that it needs to process all events on the page to see if they match the selectors provided per event type.

A better alternative (IMO) to both of those is the delegate()(docs) method which uses event delegation just like .live(), but lets you constrain it to a specific portion of the page.

$('#someContainer').delegate('a.someButton', 'click', function() {
    // do something when an "a.someButton" inside "#someContainer" is clicked
});

Note that event delegation methods respond to browser events, not to changes to the DOM. If you need to run some code based on a change to the DOM you've made, you need to run that code when you make that alteration to the DOM.



来源:https://stackoverflow.com/questions/4818020/livequery-performance

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!