livequery performance

一曲冷凌霜 提交于 2019-12-01 03:26:30

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.

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