我想知道是否可以在不引用应该删除的事件处理函数的情况下删除事件侦听器。
Jquery$().unbind('touchmove')
将不起作用,因为事件侦听器是使用 Javascript 添加的addEventListener('touchmove', handlerFunction)
,而不是 Jquery 的bind
函数。
removeEventListener('touchmove')
似乎不起作用,这与 Javascript 函数规范一致。
我想知道是否可以在不引用应该删除的事件处理函数的情况下删除事件侦听器。
Jquery$().unbind('touchmove')
将不起作用,因为事件侦听器是使用 Javascript 添加的addEventListener('touchmove', handlerFunction)
,而不是 Jquery 的bind
函数。
removeEventListener('touchmove')
似乎不起作用,这与 Javascript 函数规范一致。
您可以获取 DOM 元素的所有事件侦听器并删除一个/全部:
var eventlistener = getEventListeners(window)["DOMContentLoaded"][index];
window.removeEventListener("DOMContentLoaded", eventlistener.listener, eventlistener.useCapture);
您应该始终将事件添加到特定对象,以便以后可以访问它们,试试这个。
添加:
window.addEventHandler( 'touchmove', function );
去除:
window.removeEventListener( 'touchmove', function );