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

How to unbind an event in javascript

醉折花枝作酒筹
Release: 2023-01-06 11:17:26
Original
9627 people have browsed it

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>
Copy after login

2. Unlocking method:

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

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();
	}
Copy after login

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);
	}
Copy after login

[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!

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