javascript 使用 Chrome 的 webkit 检查器删除事件侦听器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11013943/
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
Use Chrome's webkit inspector to remove an event listener
提问by Akshat
I know you can see the event listeners in the Chrome Inspector but i'm doing some debugging work and there are so many event listeners lying around I'd like to disable some without editing the code
我知道你可以在 Chrome Inspector 中看到事件监听器,但我正在做一些调试工作,周围有太多事件监听器我想禁用一些而不编辑代码
Is there a way to disable an event listener quickly from the Webkit inspector?
有没有办法从 Webkit 检查器中快速禁用事件侦听器?
Perhaps have a look and type some code into the console to removeEventListener
the listener? How would I do this? For instance how would i remove the 'click' listener above
也许看看并removeEventListener
在监听器的控制台中输入一些代码?我该怎么做?例如,我将如何删除上面的“点击”侦听器
采纳答案by Alexander Pavlov
Sorry, you are out of luck (at least for the time being.) removeEventListener
requires the exact listener Function object as an argument, and DevTools do not let you get a grip of the listener function in any way.
抱歉,您运气不好(至少目前是这样。)removeEventListener
需要确切的侦听器 Function 对象作为参数,而 DevTools 不允许您以任何方式掌握侦听器功能。
If you definitely need this feature, please file a bug at http://new.crbug.com(against Chrome) or http://bugs.webkit.org(against WebKit, the preferred way).
如果您确实需要此功能,请在http://new.crbug.com(针对 Chrome)或http://bugs.webkit.org(针对 WebKit,首选方式)提交错误。
回答by Hymanson
You can use getEventListeners(element).click[index].listener
to get a reference to a listener (in a WebKit console).
您可以使用getEventListeners(element).click[index].listener
获取对侦听器的引用(在 WebKit 控制台中)。
So, to remove the first listener, you could do:
因此,要删除第一个侦听器,您可以执行以下操作:
document.removeEventListener('click', getEventListeners(document).click[0].listener)
Similarly, to remove all listeners, you could use this function:
同样,要删除所有侦听器,您可以使用此函数:
function removeEventListeners(element, listenerMap) {
Object.keys(listenerMap).forEach(function (name) {
var listeners = listenerMap[name];
listeners.forEach(function (object) {
element.removeEventListener(name, object.listener);
});
});
}
removeEventListeners(document, getEventListeners(document))
回答by Shaohua Li
You can remove an event listener in the javascript console.
First find the element to which this event listener is attached. Let's call it e. Then you execute:
e.onclick=null
.
For example, many event listeners are attached to "body", then the above code becomes:
document.body.onclick=null
.
After that the event listener is removed.
您可以在 javascript 控制台中删除事件侦听器。首先找到此事件侦听器附加到的元素。我们称之为e。然后你执行:
e.onclick=null
。比如很多事件监听器都附加在“body”上,那么上面的代码就变成了:
document.body.onclick=null
。之后,事件侦听器被删除。
回答by jordanbtucker
Simply:
简单地:
getEventListeners(document).click[0].remove()
This works with other elements, too:
这也适用于其他元素:
getEventListeners($('#submit-button')[0]).click[0].remove()
And other types of events:
以及其他类型的事件:
getEventListeners($('#login-form')[0]).submit[0].remove()
回答by Akyo
Maybe late but.
也许晚了但是。
In chrome there is a specific button to remove the method call of a specific event.
在 chrome 中有一个特定的按钮来删除特定事件的方法调用。
Right click on the html input (boutton in my example) inspect element, chrome opens the element viewer tab (in html mode) with another panel composed of subtabs in this panel click on the "event listener" tab and unfold the event you're intersted in (onclick in my example) put the mouse point over the name of the button you inspected and a button "remove" will apear and one clicked the event listener is deleted for the current page loaded.
右键单击 html 输入(在我的示例中为 boutton)检查元素,chrome 打开元素查看器选项卡(在 html 模式下),另一个面板由该面板中的子选项卡组成,单击“事件侦听器”选项卡并展开您正在处理的事件感兴趣(在我的示例中为 onclick)将鼠标点放在您检查的按钮名称上,然后将出现一个“删除”按钮,单击一次事件侦听器会为加载的当前页面删除。