window.onload =function(){ var oUl = document.getElementById("ul"); var oLi = oUl.getElementsByTagName("li") for(var i =0; i <oLi.length; i++){ oLi[i].onclick =function(){ alert(123) } } }
如果事件委托呢,我会把时间委托给ul 就把for 循环直接改
oUl.onclick =function(){ alert(1123) }
接下来我们想写过一个,给LI加一个鼠标经过的效果 默认我肯定会这样写:
window.onload =function(){ var oUl = document.getElementById("ul"); var oLi = oUl.getElementsByTagName("li") for(var i =0; i <oLi.length; i++){ oLi[i].onmouseover =function(){ this.style.background ="red" }