Home  >  Article  >  Web Front-end  >  How to unbind an event in javascript

How to unbind an event in javascript

醉折花枝作酒筹
醉折花枝作酒筹Original
2021-06-15 10:10:079571browse

Method: 1. Direct deletion method, use the "object.onclick=false;" statement to delete the binding event. 2. First use addEventListener to bind the event, and then use removeEventListener to delete the bound event.

How to unbind an event in javascript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

Native JS javascript unbinding event JS delete binding event

1. Direct deletion method

1. Applicable to directly bound events, such as:

<h1 id="h1" onclick="_click();">送你一朵花</h1>

2. Unlocking method:

function unbind(){
    var h1 = document.getElementById(&#39;h1&#39;);
    h1.onclick= false; // 或者 h1.onclick= null ;
}

2. First have a binding function, then unbinding method

1. First use addEventListener to bind the event

    var h1 = document.getElementById(&#39;h1&#39;);
	h1.addEventListener(&#39;click&#39;,clickx_,false);
	function clickx_(){
		alert("点击到了");
		unclick();
	}

2. Then use removeEventListener to delete the binding event

    function unclick(){
		var h1 = document.getElementById(&#39;h1&#39;);
		h1.removeEventListener(&#39;click&#39;,clickx_,false);
	}

[Recommended learning: javascript advanced tutorial

The above is the detailed content of How to unbind an event in javascript. 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