jQuery live()事件处理程序的性能 [英] Performance of jQuery live() event handler

查看:77
本文介绍了jQuery live()事件处理程序的性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑使用live()将事件处理程序绑定到我尚未插入DOM的函数中.但是,这看起来很昂贵-例如,必须在插入元素时或执行"click"元素时必须进行运行时检查,例如,以查看是否应调用处理程序.

I'm considering using live() to bind an event handler to a function I haven't inserted into the DOM. However this looks expensive - it must have to do a runtime check any time an element is inserted, or any time a "click" element is performed, for example, to see whether the handler should be called.

这是值得在实践中担心的事情,还是Javascript如此之快以至于不值得关注?

Is this something worth worrying about in practice, or is Javascript so fast now that this isn't worth caring about?

live()函数的参考页: http://api.jquery.com/live/

推荐答案

否,.live()使用事件冒泡来完成任务.它只是附加到根元素,并对在DOM树中冒泡的事件做出反应.它不会一直检查DOM元素.

No, .live() uses event bubbling to do its thing. It just attaches to the root element and reacts to events bubbling up through the DOM tree. It does not keep checking DOM elements all the time.

您从页面链接到:

.live()方法能够通过使用事件委托来影响尚未添加到DOM的元素:绑定到祖先元素的处理程序负责在其后代上触发的事件.传递给.live()的处理程序永远不会绑定到元素.相反,.live()将特殊处理程序绑定到DOM树的根.

The .live() method is able to affect elements that have not yet been added to the DOM through the use of event delegation: a handler bound to an ancestor element is responsible for events that are triggered on its descendants. The handler passed to .live() is never bound to an element; instead, .live() binds a special handler to the root of the DOM tree.

请继续阅读,以获取更多详细信息.

Keeping reading there as it goes into more detail.

这篇关于jQuery live()事件处理程序的性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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