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.
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
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('h1'); 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('h1'); h1.addEventListener('click',clickx_,false); function clickx_(){ alert("点击到了"); unclick(); }
2. Then use removeEventListener to delete the binding event
function unclick(){ var h1 = document.getElementById('h1'); h1.removeEventListener('click',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!