日期:2014-05-16 浏览次数:20393 次
< ul id=”myLinks” > < li id=”goSomewhere” > Go somewhere < /li > < li id=”doSomething” > Do something < /li > < li id=”sayHi” > Say hi < /li > < /ul >
var item1 = document.getElementById(“goSomewhere”); var item2 = document.getElementById(“doSomething”); var item3 = document.getElementById(“sayHi”); EventUtil.addHandler(item1, “click”, function(event){ location.href = “http://www.wrox.com”; }); EventUtil.addHandler(item2, “click”, function(event){ document.title = “I changed the document ’ s title”; }); EventUtil.addHandler(item3, “click”, function(event){ alert(“hi”); });
var list = document.getElementById(“myLinks”); EventUtil.addHandler(list, “click”, function(event){ event = EventUtil.getEvent(event); var target = EventUtil.getTarget(event); switch(target.id){ case “doSomething”: document.title = “I changed the document ’ s title”; break; case “goSomewhere”: location.href = “http://www.wrox.com”; break; case “sayHi”: alert(“hi”); break; } });
< div id=”myDiv” > < input type=”button” value=”Click Me” id=”myBtn” > < /div > < script type=”text/javascript” > var btn = document.getElementById(“myBtn”); btn.onclick = function(){ //do something document.getElementById(“myDiv”).innerHTML = “Processing...”; //Bad!!! }; < /script >
< div id=”myDiv” > < input type=”button” value=”Click Me” id=”myBtn” > < /div > < script type=”text/javascript” > var btn = document.getElementById(“myBtn”); btn.onclick = function(){ //do something btn.onclick = null; //remove event handler document.getElementById(“myDiv”).innerHTML = “Processing...”; }; < /script >