In JQuery when should you use .bind() over .click() - or any other given event?

浪尽此生 提交于 2019-12-12 07:34:57

问题


I understand the difference between Live and Bind but when should I use use .bind() over a 'standard' event method as shown below.

Are there any key differences in the way these two calls work?

$('.clickme').bind('click', function() {
  // Handler called.
});

$('.clickme').click(function() {
  // Handler called.
});

回答1:


They're effectively the same. However, using bind() allows you to make use of namespaced events. This is especially useful when writing plugins.




回答2:


in "bind" you can use multiple events

$('#foo').bind('mouseenter mouseleave', function() {
  $(this).toggleClass('entered');
});



回答3:


They are the same. See here




回答4:


I use the explicit methods when available. I use the bind when a method isn't available like for window.onbeforeunload

The other time to use bind is if your are developing and switching between "live" and "bind".




回答5:


use .bind when you want to bind to "event.namespace"

Actaully almost always use .bind and almost always use namespaces.



来源:https://stackoverflow.com/questions/4865566/in-jquery-when-should-you-use-bind-over-click-or-any-other-given-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!