删除由另一个脚本设置的点击事件监听器

Remove click event listener set by another script

使用 Chrome 开发人员工具,我找到了一个点击事件侦听器,我想将其删除:

如果我使用开发人员工具删除侦听器,它会起作用。现在我发现监听器是通过 jQuery:

添加的
$(".js_playerlist").on("click",".playerlist_item",function(){
    var a=$(this).hasClass("nothingThere");
    if(!a) { 
        var d=$(this).data("msgid");
        if(d) { 
            b.loadChatLogWithPlayer(this,d)
        } else {
            b.loadChatLogWithPlayer(this)
        }
    }
});

如何在不使用 jQuery 的情况下通过 Javascript 删除此事件侦听器?

您必须使用 jQuery 函数来执行此操作,因为该事件由 jquery 附加,因此请使用 unbind() or off() 这两个函数都会删除事件:

$(".js_playerlist").delay(1000).off("click",".playerlist_item");
//OR
$(".js_playerlist").delay(1000).unbind( "click.playerlist_item" );

您可以使用 javascript 方法 removeEventListener() 但您必须将要删除的函数作为参数传递,并且在您的案例中附加事件的脚本会避免这种情况。 希望这有帮助。

我想你可以使用

document.getElementsByClassName("js_playerlist")[0].removeEventListener("click", attachedFunction);

在这种情况下,attachedFunction 是事件触发时调用的附加函数。你的情况:

function(){
    var a=$(this).hasClass("nothingThere");
    if(!a) { 
        var d=$(this).data("msgid");
        if(d) { 
            b.loadChatLogWithPlayer(this,d)
        } else {
            b.loadChatLogWithPlayer(this)
        }
    }
}

可能,如果您有权访问代码,提取函数并将其分配给变量,代码会更清晰。

如果其他脚本不在你的权限范围内,为了100%易于维护,我建议你发送请求其他脚本代码并解析它以获得所需的功能,这样你就会一直有最新版本。