jquery转“开”和“关闭”事件处理程序 [英] jquery turning "on" and "off" event handlers

查看:106
本文介绍了jquery转“开”和“关闭”事件处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



ex

  var $ btn = $(#theBtn); 
var namedHandler = $ btn.on(click,function(){
// do something
// then off off
})

我会像这样关掉它

  $ btn.off( 点击); 

或者现在可以将其存储在变量中吗?

  namedHandler.off(click); 

  namedHandler.off(); 

等。



任何非常感谢的指针

解决方案

与jQuery对象相同的引用将在 $ btn namedHandler 。两者都返回同一件事情的引用,所以赋值赋值相同的东西。



你可以把它 off()任何一种方法。



可能更适合您的示例是 命名空间您的活动,因此关闭(点击,...)将不会解除所有 code>点击事件。


How would I apply the "off" directive to a named handler?

ex

var $btn = $("#theBtn");
var namedHandler = $btn.on("click", function() {
//do something
//then turn off
})

would I turn it off like this

$btn.off("click");

or could I do something else now it is stored in a variable?

namedHandler.off("click");

or

namedHandler.off();

etc.

Any pointers much appreciated.

解决方案

The same reference to the jQuery object will be in $btn and namedHandler. Both return a reference to the same thing so the assignment is assigning the same thing.

You could turn it off() with either method.

What may be more suited to your example is namespacing your event, so off('click', ...) won't unbind all click events.

这篇关于jquery转“开”和“关闭”事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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