1. 原因1. 原因许多入前端不久的人都会遇到 removeEventListener 无法清除监听的情况,这是由于要移除事件句柄,addEventListener() 的执行函数必须使用外部函数,如上实例所示 (myFunction)。匿名函数,类似 “document.removeEventListener(“event”, function(){ myScript });” 该事件是无法移除的。而在很多情况下我们需要句柄回调的传参,又需要其他传参时免不了使用句柄,这个时候我们需要写一个方法去替代这个回调,以下是解决方式,写了一个addListener 函数在 addEventListener 之后返回它的销毁方法2.解决方式2.解决方式
function isFn(value) {
const type = Object.prototype.toString.call(value);
return type === '[object Function]';
}
function isNode(value) {
return value !== undefined && value instanceof HTMLElement && value.nodeType === 1;
}

function listenNode(node, type, callback) {
node.addEventListener(type, callback);
return {

destroy() {

node.removeEventListener(type, callback);

},
};
}

function addListener(target, type, callback) {
if (!target && !type && !callback) {

throw new Error('缺少参数');
}
if (!isFn(callback)) {

throw new TypeError('Third argument must be a Function');
}
if (isNode(target)) {

return listenNode(target, type, callback);
}
throw new TypeError('First argument must be a String, HTMLElement, HTMLCollection, or NodeList');
}

function listenNodeList(nodeList, type, callback) {
Array.prototype.forEach.call(nodeList, node => {

node.addEventListener(type, callback);
});

return {

destroy() {

Array.prototype.forEach.call(nodeList, node => {

node.removeEventListener(type, callback);

});

},
};
}
module.exports = listener;

function isFn(value) {
const type = Object.prototype.toString.call(value);
return type === '[object Function]';
}
function isNode(value) {
return value !== undefined && value instanceof HTMLElement && value.nodeType === 1;
}

function listenNode(node, type, callback) {
node.addEventListener(type, callback);
return {

destroy() {

node.removeEventListener(type, callback);

},
};
}

function addListener(target, type, callback) {
if (!target && !type && !callback) {

throw new Error('缺少参数');
}
if (!isFn(callback)) {

throw new TypeError('Third argument must be a Function');
}
if (isNode(target)) {

return listenNode(target, type, callback);
}
throw new TypeError('First argument must be a String, HTMLElement, HTMLCollection, or NodeList');
}

function listenNodeList(nodeList, type, callback) {
Array.prototype.forEach.call(nodeList, node => {

node.addEventListener(type, callback);
});

return {

destroy() {

Array.prototype.forEach.call(nodeList, node => {

node.removeEventListener(type, callback);

});

},
};
}
module.exports = listener;
补充知识:vue 创建监听,和销毁监听(addEventListener, removeEventListener)补充知识:补充知识:vue 创建监听,和销毁监听(addEventListener, removeEventListener)最近在做一个有关监听scroll的功能, 发现我添加监听之后一直不起作用:
mounted() {
window.addEventListener("scroll", this.setHeadPosition); //this.setHeadPosition方法名
mounted() {window.addEventListener("scroll", this.setHeadPosition); //this.setHeadPosition方法名后来发现要在后面添加一个true之后才行:
mounted() {
window.addEventListener("scroll", this.setHeadPosition, true);
},
mounted() {
window.addEventListener("scroll", this.setHeadPosition, true);
},而在离开是的时候需要销毁监听: (在destroyed里面销毁), 否则监听会一直存在, 因为这是单页面应用, 页面并未关闭.
destroyed() {
window.removeEventListener("scroll", this.setHeadPosition, true);
},
destroyed() {
window.removeEventListener("scroll", this.setHeadPosition, true);
},在销毁的时候一定也要加上true, 否则销毁不起作用.以上这篇解决removeEventListener 无法清除监听的问题就是小编分享给大家的全部内容了,希望能给大家一个参考。