Home >Web Front-end >JS Tutorial >Code for dynamically adding events (binding events) in JS_javascript skills

Code for dynamically adding events (binding events) in JS_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:12:181026browse

Two ways: directly add events to objects, add events to nodes
For example, add an onclick event to an ID with tab1
The first case:

Copy Code The code is as follows:

var t = document.getElementById("tab1");
t.onclick = function tst(){
alert ('');
}

Second case
Copy code The code is as follows :

var tb = document.getElementById("tab1");
if(window.addEventListener){ // Mozilla, Netscape, Firefox
td_value.addEventListener('click', alert('11'), false);
td_value.addEventListener('click', alert('12'), false);
} else { // IE
td_value.attachEvent('onclick' , function(){alert('21');});
td_value.attachEvent('onclick', function(){alert('2'2);});
}
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