jQuery .on()在事件委托中有多个选择器?

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

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

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

在附加文件时是否有损失?

解决方法

>

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

06000

Yes,that will work.
>

I want to use this instead of live() given performance improvements.

与使用live()相比,使用该代码段没有性能优势,因为live()本身将事件绑定到文档,而在jQuery 1.7中,在幕后的实时调用
>

And are there any benefits lost in attaching to document?

绑定到文档的缺点是事件必须在处理之前遍历整个祖先列表;这是as pointed out in the jQuery documentation,是最慢的路线。通过将处理程序附加到更靠近事件源的元素,将更好地处理事件。

相关文章

页面搜索关键词突出 // 页面搜索关键词突出 $(function () {...
jQuery实时显示日期、时间 html: <span id=&quot...
jQuery 添加水印 <script src="../../../.....
中文:Sys.WebForms.PageRequestManagerParserErrorExceptio...
1. 用Response.Write方法 代码如下: Response.Write(&q...
Jquery实现按钮点击遮罩加载,处理完后恢复 思路: 1.点击按...