qq_加菲喵_0
2016-06-27 16:02
// 鼠标移动改变背景,可以通过给每行绑定鼠标移上事件和鼠标移除事件来改变所在行背景色。
var tr = document.getElementById("table").getElementsByTagName('tr');
for(var i=0; i<tr.length; i++)
{
tr[i].onmouseover = function(){
// tr[i].style.backgroundColor = "red";
this.style.backgroundColor = "red";
}
tr[i].onmouseout = function(){
tr[i].style.backgroundColor="white";
// this.style.backgroundColor="white";
}
}
}
tr[i].onmouseover = function(){
// tr[i].style.backgroundColor = "red";
this.style.backgroundColor = "red";
}
这里面是一个函数的调用, 在函数里面tr[i]是没有被定义的,this是可以获取标签内的对象的
懂了没
放在网页中编译一下可以发现,tr[i]没有被定义或者未被赋值。而this指的是table的tr子元素。所以二者不是同一个对象。
七八 和十一、十二行的代码
JavaScript进阶篇
469067 学习 · 22582 问题
相似问题
回答 1