FF:<html> <head> <title>DOM Event Example</title> <style type="text/css"> #t { border: 1px solid red } #t1 { background-color: pink; } </style> <script type="text/javascript"> // Function to change the content of t2 function modifyText() { var t2 = document.getElementById("t2"); t2.firstChild.nodeValue = "three"; }
// Function to add event listener to t function load() { var el = document.getElementById("t"); el.addEventListener("click", modifyText, false); } </script> </head> <body onload="load();"> <table id="t"> <tr><td id="t1">one</td></tr> <tr><td id="t2">two</td></tr> </table> </body> </html>
<head>
<title>DOM Event Example</title>
<style type="text/css">
#t { border: 1px solid red }
#t1 { background-color: pink; }
</style>
<script type="text/javascript"> // Function to change the content of t2
function modifyText() {
var t2 = document.getElementById("t2");
t2.firstChild.nodeValue = "three";
}
// Function to add event listener to t
function load() {
var el = document.getElementById("t");
el.addEventListener("click", modifyText, false);
} </script>
</head>
<body onload="load();">
<table id="t">
<tr><td id="t1">one</td></tr>
<tr><td id="t2">two</td></tr>
</table>
</body>
</html>
el.addEventListener("click", modifyText, false);
这样的写法,一般在什么情况下使用比较多哦?
}
div.onclick = function(){
}//会覆盖上一个 用addEventListener不会