如何解除特定事件处理程序的绑定

发布于 2024-09-28 12:35:26 字数 468 浏览 1 评论 0原文

代码:

$('#Inputfield').keyup(function(e)
        {
            if(e.which == 13)
            {
                functionXyz();
            }
            else
            {
                functionZyx();
            }
    });  


$(document).keyup(function(exit) {
              if (exit.keyCode == 27) { functionZzy(); }
});

问题: 如何删除 keyCode == 27 的 keyup 事件处理程序并保持其他 $(document).keyup 事件处理程序不变?

Code:

$('#Inputfield').keyup(function(e)
        {
            if(e.which == 13)
            {
                functionXyz();
            }
            else
            {
                functionZyx();
            }
    });  


$(document).keyup(function(exit) {
              if (exit.keyCode == 27) { functionZzy(); }
});

Question: How to remove the keyup event handler of keyCode == 27 and keep the other $(document).keyup event handlers intact?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

稚然 2024-10-05 12:35:27

您必须使用命名函数,以便在调用 .unbind(),像这样:

function keyUpFunc(e) {
  if (e.keyCode == 27) { functionZzy(); }
}
$(document).keyup(keyUpFunc);

然后稍后解绑时:

$(document).unbind("keyup", keyUpFunc);

You have to use a named function so you can reference that specific handler when calling .unbind(), like this:

function keyUpFunc(e) {
  if (e.keyCode == 27) { functionZzy(); }
}
$(document).keyup(keyUpFunc);

Then later when unbinding:

$(document).unbind("keyup", keyUpFunc);
坠似风落 2024-10-05 12:35:27

您将事件处理程序附加到不同的元素,因此您可以安全地从特定对象中删除处理程序(我已经提到过)。

为了完整起见,如果要将同一事件的多个处理程序附加到同一个对象,可以使用 命名空间事件

$('#Inputfield').bind('keyup.keep', function(e){/*...*/});
$('#Inputfield').bind('keyup.notkeep', function(e){/*...*/});

$('#Inputfield').unbind('keyup.notkeep');
// or event  $('#Inputfield').unbind('.notkeep');

jQuery 1.7开始,方法.on.off是添加和删除事件处理程序的首选方法。为此,它们的行为与 .bind.unbind 完全相同,并且还可以处理命名空间事件。

Your are attaching the event handlers to different elements, so you can safely remove the handler from the specific object (already mentioned I know).

For the sake of completeness, if you want to attach multiple handlers for the same event to the same object, you can use namespaced events:

$('#Inputfield').bind('keyup.keep', function(e){/*...*/});
$('#Inputfield').bind('keyup.notkeep', function(e){/*...*/});

$('#Inputfield').unbind('keyup.notkeep');
// or event  $('#Inputfield').unbind('.notkeep');

Since jQuery 1.7, the methods .on and .off are the preferred way to add and remove event handlers. For this purpose they behave exactly like .bind and .unbind and also work with namespaced events.

风透绣罗衣 2024-10-05 12:35:27

jQuery 允许您绑定在第一次调用后将解除绑定的事件。如果您只想运行此 keyup 函数一次,请查看此处列出的 .one() 方法: http:// /api.jquery.com/one/

$(document).one('keyup', function(e) {
              if (e.keyCode == 27) { functionZzy(); }
});

jQuery allows you to bind events that will be unbound after their first invocation. If you are looking to only run this keyup function once, look at the .one() method listed here: http://api.jquery.com/one/

$(document).one('keyup', function(e) {
              if (e.keyCode == 27) { functionZzy(); }
});
寻梦旅人 2024-10-05 12:35:27

如果某个元素上只有一个处理程序,则可以使用 unbind 安全地取消绑定它,而无需像 Nick Craver 建议的那样使用命名函数。在这种情况下,调用

$('#Inputfield').unbind('keyup');

不会影响 document 上的处理程序。

If you only have one handler on an element, you can safely unbind it using unbind without using named functions as Nick Craver suggests. In this case, calling

$('#Inputfield').unbind('keyup');

will not affect the handler on document.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文