jQuery:在使用delgation时链接on()

在过去,我曾经以极大的成功连接到live()的呼叫,例如:
$(".something")
    .live("click",function(e) { ... })
    .live("change",function(e) { ... })
    .live("submit",function(e) { ... });

这些天,live(),bind()和delegate()已经被闪亮的new on()取代了。

我试图用on()替换live(),这似乎是显而易见的:

$(".something")
    .on("click",function(e) { ... })
    .on("change",function(e) { ... })
    .on("submit",function(e) { ... });

但是,当您考虑如何()工作时,这几乎是显而易见的。这从http://api.jquery.com/on/

“Event handlers are bound only to the currently selected elements; they must exist on the page at the time your code makes the call to .on().”

根据jQuery文档,我需要绑定到文档并委托给.something来处理直播事件。不幸的是,这意味着如果我想用on()复制上面的代码,我最终会重复我的委托选择器(.document):

$(document)
    .on("click",".something",function(e) { ... });

这是按预期的方式工作,但我真的很希望能够像使用live()一样更清晰地链接。我错过了什么吗?

解决方法

我相信你可以这样做:
$(document).on({
    "click" : function(e) { ... },"change" : function(e) { ... },"submit" : function(e) { ... }
},".something");

也就是说,使用“events-map”语法来指定事件及其处理程序,然后指定用于委托样式行为的选择器。

相关文章

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