jQuery .on()在事件委托中有多个选择器? [英] jQuery .on() with multiple selectors in event delegation?

查看:174
本文介绍了jQuery .on()在事件委托中有多个选择器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在jQuery 1.7中使用了 .on(),并且想知道是否可以一直为已注入页面的元素添加多个选择器。以前,我正在使用 live() - 但是很明显,为什么我想提高性能。

I have using .on() in jQuery 1.7 and wondered whether it is possible to attach multiple selectors at a time for elements that have been injected onto a page. Previously, I was using live() - but it's obvious why I want to move given performance improvements.

可以您可以使用 .on(),方式如下:

Can you use .on() in the manner like:

$(document).on('click', '#selector1, #selector2, .class1', function () { 
     //stuff
});

附加文件有什么好处吗?

And are there any benefits lost in attaching to document ?

推荐答案



使用.on()方式如下:

Can you use .on() in the manner like:

$(document).on('click', '#selector1, #selector2, .class1', function () { 
    //stuff
});


是的,这将会起作用。


live()给予性能改进。

否 c(c)c $ c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c将事件绑定到文档,在jQuery 1.7中,在 c $ c>后面的现场调用

There are no performance advantages of using that code snippet as opposed to using live(), as live() itself binds events to the document, and in jQuery 1.7, live calls on behind the scenes.


附加文件有什么好处吗?

And are there any benefits lost in attaching to document?

绑定到文档是事件必须在处理之前遍历整个祖先列表;这是如jQuery文档所指出的,是最慢的路线。通过将处理程序附加到更接近事件来源的元素,将更好地处理事件。

The downside to binding to document is that the event must traverse the entire list of ancestors before it is handled; this, as pointed out in the jQuery documentation, is the slowest possible route. It will be better to handle to event sooner, by attaching the handler to an element closer to the source of the event.

这篇关于jQuery .on()在事件委托中有多个选择器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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