dom2事件优点

1.It allows adding more than a single handler for an event. This is particularly useful for DHTML libraries or Mozilla extensions that need to work well even if other libraries/extensions are used.
一个事件可以注册多个监听器

2.It gives you finer-grained control of the phase when the listener gets activated (capturing vs. bubbling)
更精细的手段控制事件监听器的触发阶段

3.It works on any DOM element, not just HTML elements.
对任何dom元素有效不仅仅是html元素

0级DOM分为2个:
一是在标签内写onclick事件

<input id="myButton" type="button" value="Press Me" onclick="alert('thanks');" >

二是在JS写onlicke=function(){}函数

以下为综合示例:

<ul id="color-list">
    <li id="addEvent">red</li>
    <li id="on_click">yellow</li>
</ul>
<script type="text/javascript">
(function(){
var addEvent = document.getElementById("addEvent");
addEvent.addEventListener("click",function(){
  alert("我是addEvent1");
},false);
addEvent.addEventListener("click",function(){
  alert("我是addEvent2");
},false);

var addEvent = document.getElementById("on_click"); 
on_click.onclick = function() {
  alert("我是click1");
}
on_click.onclick = function() {
  alert("我是click2");
}
})();
</script>
作者 铁血 汉子 2019年3月29日
2024/12/22/07:23:41am 2019/3/29/11:45:16
0 1572