jquery使用bind vs on click [英] jquery use of bind vs on click

查看:145
本文介绍了jquery使用bind vs on click的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在jquery中遇到过几种处理点击事件的方法:

I have come across several methods for handling click events in jquery:

bind:

$('#mydiv').bind('click', function() {
    ...
});

点击:

$('#mydiv').click(function() {
   ...
}

on:

$('mydiv').on('click', function() {
   ...
}

两个问题:


  1. 他们还有其他办法吗?

  2. 我应该使用哪一个,为什么?

更新:

正如eveyone提出的建议,我应该阅读文档更好,并发现我应该使用:

As eveyone has helpfully suggested, I should have read the docs better, and found out that I should use:

on()或click(),

on() or click(),

这些是实际上是同样的事情。

which are effectively the same thing.

然而,没有人解释为什么不再推荐绑定?我可能会因为错过明显的某处而得到更多的downvotes,但我不能在文件中找到原因。

However, nobody has explained why bind is no longer recommended ? I'll probably get more downvotes for missing the obvious somewhere, but I can't find a reason for this in the documents.

UPDATE2:

'on'具有有用的效果能够为动态添加事件处理程序创造了元素。例如

'on' has the useful effect of being able to add event handlers to dynamically created elements. e.g.

$('body').on('click',".myclass",function() {
    alert("Clicked On MyClass element");
});

此代码为具有myClass类的元素添加了一个单击处理程序。但是,如果稍后动态添加更多myClass元素,它们也会自动获得点击处理程序,而不必显式调用'on'。根据我的理解人们说,这也更有效(参见下面的Simons答案)。

This code adds a click handler to elements with a class of 'myClass'. However, if more myClass elements are then dynamically added later, they automatically get the click handler as well, without having to explicitly call 'on'. From what I understand people are saying, this is also more efficient (see Simons answer below).

推荐答案

来自<的文档code> bind 和点击

绑定

bind :


截至jQuery 1.7,.on()方法是
将事件处理程序附加到文档的首选方法。

As of jQuery 1.7, the .on() method is the preferred method for attaching event handlers to a document.

来源表明没有理由使用 bind ,此函数仅在函数上调用更灵活的,甚至不会更短:

The source makes it clear there's no reason to use bind, as this function only calls the more flexible on function without even being shorter :

bind: function( types, data, fn ) {
    return this.on( types, null, data, fn );
},

所以我建议,就像jQuery团队一样,忘记旧的 bind 函数,现在没用了。这只是为了与旧代码的兼容性,它仍然在这里。

So I'd suggest, just like the jQuery team, to forget the old bind function, which is now useless. It's only for compatibility with older code that it's still here.

点击

click :


此方法是.on('click',handler)的快捷方式

This method is a shortcut for .on('click', handler)

此快捷方式当然不如上的功能强大且灵活,且不允许委托但它允许您在应用时编写更短且可以说更易读的代码。关于这一点的意见分歧:一些开发人员认为应该避免它,因为它只是一个捷径,并且还有一个事实是你需要在上用替换它你使用委托,为什么不直接使用 更加一致?

This shortcut is of course less powerful and flexible than the on function and doesn't allow delegation but it lets you write a shorter and, arguably, slightly more readable, code when it applies. Opinions diverge on that point : some developers argue that it should be avoided as it is just a shortcut, and there's also the fact that you need to replace it with on as soon as you use delegation, so why not directly use on to be more consistent ?

这篇关于jquery使用bind vs on click的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆