仅仅一个问题:有没有办法完全移除对象的所有事件,例如一个div?
编辑:我正在添加一个事件 div.addEventListener('click',eventReturner(),false);
function eventReturner() {
return function() {
dosomething();
};
}
编辑2:我找到了一种方法,它可以工作,但不可能用于我的情况:
var returnedFunction;
function addit() {
var div = document.getElementById('div');
returnedFunction = eventReturner();
div.addEventListener('click',returnedFunction,false); //You HAVE to take here a var and not the direct call to eventReturner(), because the function address must be the same, and it would change, if the function was called again.
}
function removeit() {
var div = document.getElementById('div');
div.removeEventListener('click',returnedFunction,false);
}
div.onclick=null
进行移除。当然,在这种情况下,您不应该使用addEventListener
,因为它会添加一个不同于onclick
中的监听器。 - venimus