Home > Web Front-end > JS Tutorial > body text

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

WBOY
Release: 2016-05-16 18:12:18
Original
990 people have browsed it

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);});
}
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