Home  >  Article  >  Web Front-end  >  javascript how to uncheck event

javascript how to uncheck event

王林
王林Original
2021-11-10 14:24:404563browse

Javascript method to uncheck the event: You can uncheck the event by deleting the event handler, such as [btn.onclick = null;].

javascript how to uncheck event

#The operating environment of this article: windows10 system, javascript 1.8.5, thinkpad t480 computer.

Event handlers are divided into DOM0 level and DOM2 level. If the event is bound by onclick, it can be canceled by the following method:

btn.onclick=null;//删除事件处理程序

If you use the addEventListener() method to add an event, you can use When removeEventListener() removes an event, two points need to be noted:

1. The third parameter of removeEventListener() must be consistent with the third parameter of the addEventListener() method.

2. Anonymous functions added through the addEventListener() method cannot be removed.

btn.aaddEventListener('click',function(){alert(1);},false);
btn.removeEventListener('click',function(){alert(1);},false);//没有用!

aaddEventListener and removeEventListener seem to pass in the same parameters, but in fact the second parameter of removeEventListener and the second parameter of aaddEventListener are completely different functions!
You must do this if you want to move out

var fn=function(){
    alert(1);
};
btn.aaddEventListener('click',fn,false);
btn.removeEventListener('click',fn,false);//有效

Recommended learning: javascript video tutorial

The above is the detailed content of javascript how to uncheck event. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn