如:给ul下的li(或li下的XX)绑定事件
<ul id="ul1"> <li><span>1</span></li> <li>2</li> </ul> <script type="text/javascript"> function addEventHandler(elm,eventType,handler){ elm=typeof elm=="string"?document.getElementById(elm):elm; if(elm.attachEvent){ elm.attachEvent("on"+eventType,handler); }else if(elm.addEventListener){ elm.addEventListener(eventType,handler,false); }else return false; } function getEventTarget(e){ e=window.event||e; return e.srcElement||e.target; } var ulNode=document.getElementById("ul1"); addEventHandler(ulNode,"click",function(e){alert(getEventTarget(e).nodeName)}); </script>
单击"1",显示"SPAN"
单击"2",显示"LI"