Home >Web Front-end >JS Tutorial >The anonymous function of javascrpt binding event cannot be unbound_Basic knowledge

The anonymous function of javascrpt binding event cannot be unbound_Basic knowledge

WBOY
WBOYOriginal
2016-05-16 17:47:231251browse

I often hear people say that anonymous function binding events are difficult to control, cannot be unbound, etc. I have always been surprised who said that they cannot be unbound.

The following is to implement automatic unbinding after the click event occurs twice.

Look at the code:

Copy the code The code is as follows:

var dom=document.getElementById("test"),clickt=0;
dom.addEventListener("click",function(e){
clickt;
alert('you Touched me' clickt ', please touch it 2 times at most');
if(clickt>=2){
this.removeEventListener(e.type,arguments.callee,false);
}
});

Many extension libraries, such as jquery, can implement custom unbinding like this:
Copy code The code is as follows:

var t=0;
$(".a").bind("click",function(e){
t ;
alert('You touched me' t 'down. Touch 2 times at most');
if(t>=2){
$(this).unbind(e.type ,arguments.callee);
}
});

Test the others yourself.
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