Use Chrome's webkit inspector to remove an event listener

冷暖自知 提交于 2019-11-29 07:42:03

问题


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

Is there a way to disable an event listener quickly from the Webkit inspector?

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


回答1:


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.

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).




回答2:


You can use getEventListeners(element).click[index].listener to get a reference to a listener (in a WebKit console).

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))



回答3:


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.




回答4:


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()



回答5:


Maybe late but.

In chrome there is a specific button to remove the method call of a specific event.

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.



来源:https://stackoverflow.com/questions/11013943/use-chromes-webkit-inspector-to-remove-an-event-listener

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!