通常情况下,比如表格里面,我们需要在鼠标移动到某行上面的时候,对该行进行变色处理。这个时候就是
tr.onmouseover = function(ev){
this.style.backgroundColor = "#fafafa";
}
tr.onmouseout = function(ev){
this.style.backgroundColor = "none";
}
需要为每一行添加这样一个监视;
但是如果采用事件代理的话,就只需要为整个表格添加这样的监视
table.onmouseover = function(ev){
ev = ev||window.event;
var target = ev.target || ev.srcElement;
target.parentNode.style.backgroundColor = "#fafafa";
}
table.onmouseout= function(ev){
ev = ev||window.event;
var target = ev.target || ev.srcElement;
target.parentNode.style.backgroundColor = "none";
}
tr.onmouseover = function(ev){
this.style.backgroundColor = "#fafafa";
}
tr.onmouseout = function(ev){
this.style.backgroundColor = "none";
}
需要为每一行添加这样一个监视;
但是如果采用事件代理的话,就只需要为整个表格添加这样的监视
table.onmouseover = function(ev){
ev = ev||window.event;
var target = ev.target || ev.srcElement;
target.parentNode.style.backgroundColor = "#fafafa";
}
table.onmouseout= function(ev){
ev = ev||window.event;
var target = ev.target || ev.srcElement;
target.parentNode.style.backgroundColor = "none";
}