实时查询性能 [英] livequery performance

查看:113
本文介绍了实时查询性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近已发现 jQuery的livequery插件可能很浪费,因为它不使用事件委托,而是绑定所有可绑定的事件,并在每次更改时重新检查整个DOM.

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

如果有人对使用livequery和.live()的最佳做法有更多信息或建议,我将不胜感激

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

推荐答案

实际上很少需要像livequery这样的插件.也许唯一真正需要的时间是,如果您需要对无法修改的其他jQuery代码所做的DOM更改做出反应.

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.

.live()使用事件委托时,它在document级别上进行,这意味着它需要处理页面上的 all 事件,以查看它们是否与每个事件提供的选择器匹配.事件类型.

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.

delegate() (docs ) 方法,该方法与.live()一样使用事件委派,但可以将其限制在页面的特定部分.

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
});

请注意,事件委托方法响应浏览器事件,而不响应DOM更改.如果您需要基于对DOM所做的更改来运行某些代码,则需要在对DOM进行更改后运行该代码.

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.

这篇关于实时查询性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆