假设我只想将一个简单的点击处理程序附加到一个元素,我总是这样做:
$("#mydiv").click(function() { ... });
看看JQuery文档,似乎.on()
是附加事件处理程序并替换.bind(),. delegate()和.live()的“推荐”方式:
As of jQuery 1.7,the .on() method provides all functionality required
for attaching event handlers. For help in converting from older jQuery
event methods,see .bind(),.delegate(),and .live().
在.click()
的文档中,它说:
This method is a shortcut for .bind(‘click’,handler) in the first two variations,and .trigger(‘click’) in the third.
所以这意味着.click()正在使用.bind(),它将被弃用并替换为.on(),对吧?或者暗示.click()会坚持下去,但它会在某些时候成为.on(“点击”)的捷径?
基本上,我的问题是这个…今天编写JQuery代码时,我应该使用:
Variant 1: $("#mydiv").click(function() { ... });
要么:
Variant 2: $("#mydiv").on("click",function() { ... });
?
最佳答案