简体   繁体   English

.unbind().click(function() {...}) 是一个好习惯吗?

[英]Is .unbind().click(function() {…}) a good practice?

I'm talking about the following:我正在谈论以下内容:

.unbind().click(function () {
    // Do something
}

It looks a bit dodgy to me, however it makes sense: the developer wants to first remove any events bound to the element and then bind a click event.对我来说它看起来有点狡猾,但它是有道理的:开发人员希望首先删除绑定到元素的任何事件,然后绑定一个单击事件。

Is there a better solution for this issue?这个问题有更好的解决方案吗? Or correct my way of thinking.或者纠正我的思维方式。

It's not really good practice, because you do not control which click events get unbound.这不是很好的做法,因为您无法控制哪些点击事件被解除绑定。 There are two different approaches to improve this:有两种不同的方法可以改善这一点:


Use event namespaces (faster)使用事件命名空间(更快)

You can use a namespace to control you unbind just the click event you're going to bind again:您可以使用命名空间来控制您取消绑定您将再次绑定的点击事件:

$(selector).off('click.namespace').on('click.namespace', function(e) { //...

Use classes (fastest)使用类(最快)

Use classes added to the link to mark it as registered (this does not unbind previously bound events, but it helps preventing multiple event bindings, which is the issue in most cases you would use off (unbind) before on (bind):添加到链接使用类将其标记为注册(这并不解除绑定以前绑定的事件,但它有助于防止多个事件绑定,这是在大多数情况下,这个问题你应该使用off前(解除绑定) on (绑定):

$(selector).not('.registered').addClass('registered').on('click', function(e) { //...

You can even turn this into a little sexy plugin, writing:你甚至可以把它变成一个性感的插件,写:

$.fn.register = function(register_class) {
  register_class || (register_class = 'registered'); // lets you control the class
  return this.not('.' + register_class).addClass(register_class);
};

Like this, you can just call register on every selector:像这样,您可以在每个选择器上调用register

$(selector).register().on('click', function(e) { //...

Or with a specific class, if «registered» is taken:或者使用特定的类,如果 «registered» 被采用:

$(selector).register('special_handler_registered').on('click', function(e) { //...


Performance?表现?

If you wonder about the performance of the different handlers: 如果您想知道不同处理程序的性能:

Check out this performance test here在此处查看此性能测试

I followed this approach whenever I had to "renew" some button, link, ect., behavior, and I think there's nothing wrong with it.每当我不得不“更新”某些按钮、链接等行为时,我都遵循这种方法,我认为它没有任何问题。 But be aware that with your code you'd be removing every handler attached to the element(s).但请注意,使用您的代码,您将删除附加到元素的每个处理程序。 So, instead:所以与其:

$(selector).unbind('click').click(function(){
    // do something
})

Well, at least it is good practice to specify which event handlers should be unbinded.好吧,至少指定哪些事件处理程序应该解除绑定是一种很好的做法。 So if there was some click event hendler and we want to unbind only it, then we can use unbind('click') .所以如果有一些点击事件处理程序并且我们只想解除绑定它,那么我们可以使用unbind('click')

As there can be some other handlers in addition to yours, it make sense to use namespaces.由于除了您的处理程序之外还有其他一些处理程序,因此使用命名空间是有意义的。

$(selector).off('click.myns').on('click.myns', function() {....})

or 

$(selector).unbind('click.myns').bind('click.myns',function() {...})

This way you will only touch your own handler and not some others, for instance added by jquery plugins这样你只会接触你自己的处理程序而不是其他一些处理程序,例如由 jquery 插件添加

I usually try to used named event functions where ever possible in order to be able to unbind them explicitly:我通常会尽可能使用命名事件函数,以便能够显式解除它们的绑定:

$('a').unbind('click.myfunc').bind('click.myfunc', function(evt) { ... });

This way you could add this binding to an init-function that could be executed multiple times (handy for situations where you can't use delegate for whatever reason).通过这种方式,您可以将此绑定添加到可以多次执行的 init 函数中(对于出于任何原因无法使用委托的情况,这很方便)。

In general I would't try to unbind every event or even every handler of a certain event if I don't need to.一般来说,如果我不需要,我不会尝试解除绑定每个事件甚至某个事件的每个处理程序。

I'm also trying to stay current and replace all the bind/unbind calls with on/off ;-)我也在努力保持最新状态并用开/关替换所有绑定/取消绑定调用;-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM