以下代码有什么区别吗?
$('#whatever').on('click', function() {
/* your code here */
});
并且$('#whatever').click(function() {
/* your code here */
});
我认为,它们之间的区别在于使用模式。
我更喜欢使用.on
而不是.click
,因为前者可以使用更少的内存,并且适用于动态添加的元素。
考虑以下HTML:
<html>
<button id="add">Add new</button>
<div id="container">
<button class="alert">alert!</button>
</div>
</html>
我们是通过以下方式添加新按钮的
$("button#add").click(function() {
var html = "<button class='alert'>Alert!</button>";
$("button.alert:last").parent().append(html);
});
如果我们想要在点击时弹出警告框,可以使用 "click" 或 "on"。
click
时$("button.alert").click(function() {
alert(1);
});
使用上述方法,会为匹配选择器的每个单独元素创建一个单独的处理程序。这意味着:
.on
时$("div#container").on('click', 'button.alert', function() {
alert(1);
});
使用上述方法,一个用于匹配选择器的单一处理程序可以处理所有匹配元素,包括动态创建的元素。
.on
的另一个原因正如Adrien在下面评论中提到的,使用.on
的另一个原因是命名空间事件。
如果你使用.on("click", handler)
添加一个处理程序,通常可以使用.off("click", handler)
将其删除,这将删除该处理程序。显然,这仅在您有对该函数的引用时才有效,那么如果没有呢?您可以使用命名空间:
$("#element").on("click.someNamespace", function() { console.log("anonymous!"); });
通过解绑实现
$("#element").off("click.someNamespace");
on('click' ...)
时另一个好处是可以使用命名空间,例如on('click.darkenallothersections' ...)
, 同时也可以有on('click.displaynextstep' ...)
,然后可以使用.unbind('click.displaynextstep')
来仅解除所选的绑定。 - Adriano.on()
方法。 - gilly3selector
参数时,才会看到性能提升。如果您调用.on()
而没有使用selector
参数,则与使用.click()
相比,不会有性能改进。 - gilly3.on()
是jQuery 1.7推荐的事件绑定方式,它将.bind()
和.live()
的所有功能合并到一个函数中,根据传递的不同参数来改变行为。
在你编写的示例中,两者之间没有区别。都将处理程序绑定到#whatever
的click
事件上。 on()
提供了额外的灵活性,允许您将#whatever
子元素触发的事件委托给单个处理程序函数,如果您选择这样做。
// Bind to all links inside #whatever, even new ones created later.
$('#whatever').on('click', 'a', function() { ... });
.live()
所做的吗?此外,这似乎与其他答案相矛盾,其他答案说它只具有.click()
的功能,因此不适用于未来事件。 - Ben G.on()
可以做到.click()
所能做的事情,并且还可以像.bind()
和.live()
一样使用-这取决于您使用的参数。 (其他答案也提到了这一点。)请注意,“绑定到#whatever内的所有链接”不是.live()
所做的,而是.delegate()
所做的。 .live()
绑定到整个document
而不是让你指定容器。还请注意,从jQuery 1.7开始,.live()
已被弃用。 - nnnnnn正如其他答案所提到的:
$("#whatever").click(function(){ });
// is just a shortcut for
$("#whatever").on("click", function(){ })
需要注意的是,.on()
支持多种其他参数组合,而 .click()
则不支持,这使得它能够处理事件委托(取代了.delegate()
和.live()
)。
(显然,还有其他类似的 "keyup"、"focus" 等快捷方式方法。)
我发表额外答案的原因是提到在不带参数调用 .click()
时会发生什么:
$("#whatever").click();
// is a shortcut for
$("#whatever").trigger("click");
请注意,如果您直接使用.trigger()
,则还可以传递额外的参数或jQuery事件对象,而.click()
则无法实现。
我还想提到的是,如果您查看jQuery源代码(在jquery-1.7.1.js中),您将看到内部.click()
(或.keyup()
等)函数实际上将调用.on()
或.trigger()
。显然,这意味着您可以确保它们真正具有相同的结果,但这也意味着使用.click()
会多出一点点开销——在大多数情况下,这没有任何问题或需要考虑的,但从理论上讲,在特殊情况下可能会有所影响。
编辑:最后,请注意,.on()
允许您在一行中将多个事件绑定到同一个函数,例如:
$("#whatever").on("click keypress focus", function(){});
.click
事件只有在元素被渲染时才能起作用,并且仅附加到在DOM准备就绪时加载的元素上。
.on
事件是动态附加到DOM元素上的,这对于想要将事件附加到在ajax请求或其他情况下呈现的DOM元素上非常有用(在DOM准备就绪后)。
在这里,您将得到应用点击事件的不同方式列表。您可以根据需要选择适当的方法,或者如果您的点击不起作用,可以尝试其中的替代方法。
$('.clickHere').click(function(){
// this is flat click. this event will be attatched
//to element if element is available in
//dom at the time when JS loaded.
// do your stuff
});
$('.clickHere').on('click', function(){
// same as first one
// do your stuff
})
$(document).on('click', '.clickHere', function(){
// this is diffrent type
// of click. The click will be registered on document when JS
// loaded and will delegate to the '.clickHere ' element. This is
// called event delegation
// do your stuff
});
$('body').on('click', '.clickHere', function(){
// This is same as 3rd
// point. Here we used body instead of document/
// do your stuff
});
$('.clickHere').off().on('click', function(){ //
// deregister event listener if any and register the event again. This
// prevents the duplicate event resistration on same element.
// do your stuff
})
on()
的重点在于其其他重载以及处理没有快捷方法的事件的能力。