.on('click') 与 .click() 之间的区别
下面的代码有什么区别吗?
$('#whatever').on('click', function() {
/* your code here */
});
和
$('#whatever').click(function() {
/* your code here */
});
Is there any difference between the following code?
$('#whatever').on('click', function() {
/* your code here */
});
and
$('#whatever').click(function() {
/* your code here */
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
我认为,区别在于使用模式。
我更喜欢
.on
而不是.click
,因为前者可以使用更少的内存并可用于动态添加的元素。考虑以下 html:
我们通过其中添加新按钮
并希望“警报!”显示警报。我们可以使用“click”或“on”来实现。
当我们
在上面使用
click
时,将为与选择器匹配的每个元素创建一个单独的处理程序。这意味着当我们将
.on
与上面的内容一起使用时,单个处理程序将处理与您的选择器匹配的所有元素,包括动态创建的元素。
...使用
.on
的另一个原因正如 Adrien 在下面评论的那样,使用
.on
的另一个原因是命名空间事件。如果您使用
.on("click", handler)
添加处理程序,通常会使用.off("click", handler)
删除它,这将删除该处理程序。显然,只有当您有对该函数的引用时,这才有效,那么如果没有呢?您使用命名空间:通过解除绑定
I think, the difference is in usage patterns.
I would prefer
.on
over.click
because the former can use less memory and work for dynamically added elements.Consider the following html:
where we add new buttons via
and want "Alert!" to show an alert. We can use either "click" or "on" for that.
When we use
click
with the above, a separate handler gets created for every single element that matches the selector. That means
When we use
.on
with the above, a single handler for all elements that match your selector, including the ones created dynamically.
...another reason to use
.on
As Adrien commented below, another reason to use
.on
is namespaced events.If you add a handler with
.on("click", handler)
you normally remove it with.off("click", handler)
which will remove that very handler. Obviously this works only if you have a reference to the function, so what if you don't ? You use namespaces:with unbinding via
不,您问题中的两个代码示例之间没有功能差异。
.click(fn)
是.on("click", fn)
的“快捷方法”。来自.on()
的文档:请注意,
.on()
与.click()
的不同之处在于它能够创建 通过传递selector
参数来委派事件处理程序,而.click()
则不会。当在没有selector
参数的情况下调用.on()
时,其行为与.click()
完全相同。如果您想要事件委托,请使用.on()
。No, there is no functional difference between the two code samples in your question.
.click(fn)
is a "shortcut method" for.on("click", fn)
. From the documentation for.on()
:Note that
.on()
differs from.click()
in that it has the ability to create delegated event handlers by passing aselector
parameter, whereas.click()
does not. When.on()
is called without aselector
parameter, it behaves exactly the same as.click()
. If you want event delegation, use.on()
.自 jQuery 1.7 起,推荐使用
.on()
进行所有事件绑定。它将.bind()
和.live()
的所有功能整合到一个函数中,当您向其传递不同的参数时,该函数会改变行为。正如您所写的示例一样,两者之间没有区别。两者都将处理程序绑定到
#whatever
的click
事件。on()
提供了额外的灵活性,允许您将#whatever
的子级触发的事件委托给单个处理函数(如果您选择)。.on()
is the recommended way to do all your event binding as of jQuery 1.7. It rolls all the functionality of both.bind()
and.live()
into one function that alters behavior as you pass it different parameters.As you have written your example, there is no difference between the two. Both bind a handler to the
click
event of#whatever
.on()
offers additional flexibility in allowing you to delegate events fired by children of#whatever
to a single handler function, if you choose.正如其他答案所提到的:
请注意,尽管
.on()
支持.click()
不支持的其他几个参数组合,允许它处理事件委托(取代.delegate()
和.live()
)。(显然还有其他类似的“keyup”、“focus”等快捷方法。)
我发布额外答案的原因是提及如果您使用
.click()
调用会发生什么无参数:请注意,如果直接使用
.trigger()
,您还可以传递额外的参数或 jQuery 事件对象,这是.click()
无法做到的。我还想提一下,如果您查看 jQuery 源代码(在 jquery-1.7.1.js 中),您会在内部看到
.click()
(或.keyup( )
等)函数实际上会调用.on()
或.trigger()
。显然,这意味着您可以放心,它们确实具有相同的结果,但这也意味着使用.click()
的开销稍多一些 - 在大多数情况下无需担心或什至考虑但从理论上讲,在特殊情况下这可能很重要。编辑:最后,请注意
.on()
允许您将多个事件绑定到一行中的同一函数,例如:As mentioned by the other answers:
Noting though that
.on()
supports several other parameter combinations that.click()
doesn't, allowing it to handle event delegation (superceding.delegate()
and.live()
).(And obviously there are other similar shortcut methods for "keyup", "focus", etc.)
The reason I'm posting an extra answer is to mention what happens if you call
.click()
with no parameters:Noting that if you use
.trigger()
directly you can also pass extra parameters or a jQuery event object, which you can't do with.click()
.I also wanted to mention that if you look at the jQuery source code (in jquery-1.7.1.js) you'll see that internally the
.click()
(or.keyup()
, etc.) function will actually call.on()
or.trigger()
. Obviously this means you can be assured that they really do have the same result, but it also means that using.click()
has a tiny bit more overhead - not anything to worry or even think about in most circumstances, but theoretically it might matter in extraordinary circumstances.EDIT: Finally, note that
.on()
allows you to bind several events to the same function in one line, e.g.:.click
事件仅在元素渲染时起作用,并且仅附加到 DOM 准备就绪时加载的元素。.on
事件动态附加到 DOM 元素,当您想要将事件附加到通过 ajax 请求或其他方式呈现的 DOM 元素(在 DOM 准备好之后)时,这非常有用。.click
events only work when element gets rendered and are only attached to elements loaded when the DOM is ready..on
events are dynamically attached to DOM elements, which is helpful when you want to attach an event to DOM elements that are rendered on ajax request or something else (after the DOM is ready).在这里您将获得应用点击事件的不同方法的列表。您可以相应地选择合适的,或者如果您的点击不起作用,只需尝试其中的替代方案。
Here you will get list of diffrent ways of applying the click event. You can select accordingly as suaitable or if your click is not working just try an alternative out of these.
不,没有。
on()
的要点是它的其他重载,以及处理没有快捷方法的事件的能力。No, there isn't.
The point of
on()
is its other overloads, and the ability to handle events that don't have shortcut methods.它们似乎是相同的...来自 click() 函数的文档:
此方法是on() 函数中的
They appear to be the same... Documentation from the click() function:
Documentation from the on() function:
他们现在已经弃用了 click(),所以最好使用 on('click')
They've now deprecated click(), so best to go with on('click')
据我从互联网和一些朋友了解到,当动态添加元素时使用 .on() 。但是当我在一个简单的登录页面中使用它时,单击事件应该将 AJAX 发送到 node.js 并在返回时附加新元素,它开始调用多 AJAX 调用。当我将其更改为 click() 时,一切正常。事实上我以前没有遇到过这个问题。
As far as ilearned from internet and some friends .on() is used when you dynamically add elements. But when i used it in a simple login page where click event should send AJAX to node.js and at return append new elements it started to call multi-AJAX calls. When i changed it to click() everything went right. Actually i did not faced with this problem before.
$(document).on('click','.UPDATE',function( ){ });
与使用 jquery 获取的数据一样有效。更新或删除时按钮不起作用:$(document).on('click','.UPDATE',function(){ });
is effectively work like same fetched data by using jquery. button not working at time of update or delete:新元素
作为上述综合答案的附录,如果您希望单击附加到新元素,请突出显示关键点:
NEW ELEMENTS
As an addendum to the comprehensive answers above to highlight critical points if you want the click to attach to new elements: