Home > Web Front-end > JS Tutorial > body text

javascript how to uncheck event

王林
Release: 2021-11-10 14:24:40
Original
4556 people have browsed it

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;//删除事件处理程序
Copy after login

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);//没有用!
Copy after login

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);//有效
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!