javascript 使用 jquery 从窗口对象中删除事件侦听器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18947653/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 13:47:13  来源:igfitidea点击:

Remove event listener from window object using jquery

javascriptjquerybrowser

提问by Varun Jain

I am trying to remove blurand focusevent listeners from window object using jquery's unbind function using:

我正在尝试使用 jquery 的 unbind 函数从 window 对象中删除blurfocus事件侦听器:

function removeWindowEvents(){
    $(window).unbind('blur') ; 
    $(window).unbind('focus') ;
}

I registered the event using Javascript:

我使用 Javascript 注册了该事件:

function addEvents(){
window.addEventListener('blur', function(){ document.title = "Blurred" ; });
window.addEventListener('focus', function(){ document.title = "In Focus" ;}); 


}

This however does not work. What am I doing wrong? I tested this is Mozilaa and Chrome(latest versions)

然而这不起作用。我究竟做错了什么?我测试了这是 Mozilaa 和 Chrome(最新版本)

采纳答案by jfriend00

You can't do it your way.

你不能按照你的方式去做。

jQuery can only unbind all event handlers for a given event if the original listeners were configured using jQuery.

如果原始侦听器是使用 jQuery 配置的,则 jQuery 只能解除给定事件的所有事件处理程序的绑定。

This is because an event that is added with addEventListener()must be removed with removeEventListener()and removeEventListener()requires a second argument that specifies the callback function.

这是因为添加 with 的事件addEventListener()必须删除 withremoveEventListener()并且removeEventListener()需要指定回调函数的第二个参数。

From the MDN page:

MDN 页面

target.removeEventListener(type, listener[, useCapture])

If the event is originally registered using jQuery, jQuery works around this by having only one master event registered with addEventListener that points to it's own callback function and then using it's own event dispatching to all the events registered via jQuery. This allows it to support generic .unbind()like you're trying to use, but it will only work if the original event is registered with jQuery and thus goes through the jQuery event handler management system.

如果该事件最初是使用 jQuery 注册的,则 jQuery 可以通过仅使用 addEventListener 注册一个主事件来解决此问题,该主事件指向它自己的回调函数,然后使用它自己的事件调度到通过 jQuery 注册的所有事件。这允许它.unbind()像您尝试使用的那样支持泛型,但只有当原始事件注册到 jQuery 并因此通过 jQuery 事件处理程序管理系统时,它才会起作用。

So, without jQuery, you would do it like this:

所以,如果没有 jQuery,你会这样做:

function blurHandler() {
    document.title = "Blurred" ;
}

function focusHandler() {
    document.title = "In Focus" ;
}

function addEvents(){
    window.addEventListener('blur', blurHandler);
    window.addEventListener('focus', focusHandler); 
}

function removeWinowEvents() {
    window.removeEventListener('blur', blurHandler);
    window.removeEventListener('focus', focusHandler);
}

With jQuery, you could do it like this:

使用 jQuery,你可以这样做:

function addEvents(){
    $(window).on('blur', function(){ document.title = "Blurred" ; })
             .on('focus', function(){ document.title = "In Focus" ;}); 
}

function removeWindowEvents() {
    $(window).off('blur focus');
}

回答by kasper Taeymans

try binding with jquery see Using JQuery to bind "focus" and "blur" functions for "window", doesn't work in IE

尝试使用 jquery 绑定,请参阅使用 JQuery 绑定“窗口”的“焦点”和“模糊”函数,在 IE 中不起作用

$(function() {
    $(window).focus(function() {
        console.log('Focus');
    });

    $(window).blur(function() {
        console.log('Blur');
    });
});