<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> <script type="text/javascript"> window.onload = function () { var list = document.getElementById("myLinks"); list.onclick = function (event) { var target = event.target; switch (target.id) { case "doSomething": document.title = "I change the document‘s title"; break; case "goSomewhere": location.href = "http://baidu.com"; break; case "sayHi": alert("hi"); break; } } } </script> </head> <body> <ul id="myLinks"> <li id="goSomewhere">Go somewhere</li> <li id="doSomething">Do something</li> <li id="sayHi">Say hi</li> </ul> </body> </html> 这样的好处是,不需要为3个li 增加Onclick事件,从而节约内存
时间: 2024-10-11 03:35:28