A-A+
委托模式
多个对象接受并处理统一请求,他们将请求委托给另一个对象统一处理请求。
点击日历中的每个格子,使其背景色改变。
var ul=document.getElementById('container'),
li =document.getElementsByTagName('li'),
i=li.length-1;
for (;i>=0;I--){
li[i].onclick=function(){
this.backgroundColor="red";
}
}
改进
ul.onclick=function(e){
var e=e||window.event,
tar=e.target||e.srcElement;
if (tar.nodeName.toLowerCase()==="li"){
tar.style.backgroundColor="red";
}
}
预言未来
对未来的元素绑定事件。
<div id="art">
<p>哈哈哈哈</p>
</div>