Possible Duplicate:
What's the difference betweenon
andlive
orbind
?
我注意到有人提到 jQuery 的 .on()
绑定(bind)事件的方法。我查了一下文档,看起来它是 1.7 版本的新内容。我环顾四周,找不到为什么应该使用它以及它与 .bind()
有什么不同。 。我可以使用 .on()
现在都是这样还是针对特定情况?
如果有人有任何意见或链接来解释其中的差异,我将不胜感激。
最佳答案
您现在可以对所有事件绑定(bind)使用on
。正如文档所述:
As of jQuery 1.7, the .on() method provides all functionality required for attaching event handlers.
您可以像使用bind
一样使用它:
$(elem).on("click", function() {
//Do stuff
});
您也可以像使用delegate
一样使用它:
$(elem).on("click", "selector", function() {
//Do stuff
});
关于jQuery on() 事件绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8152909/