為什么鼠標(biāo)移動(dòng)顏色沒變化?
? window.onload = function(){
? ? ? ? ? ? ? ? ? var h=document.getElementsByTagName("tr");
? ? ?// 鼠標(biāo)移動(dòng)改變背景,可以通過給每行綁定鼠標(biāo)移上事件和鼠標(biāo)移除事件來改變所在行背景色。
? ? ? ? ? ? ? ? ?// alert(h.length);
? ? ? ? ? ? ? ? ? for (var i=1;i<h.length;i++) {
? ? ? ? ? ? ? ? ? h[i].onmousemove=move(h[i]);
? ? ? ? ? ?
? ? ? ? ? ? ? ? ? }?
}
? ? ?
? ? ?function move(obj)
? ? ?{
? ? ? obj.style.backgroundColor="#f2f2f2";
? ? ?}
? ? ?
2016-11-15
var h=document.getElementsByTagName("tr")[0]; 使用ByTagName要通過下標(biāo)位置來獲取到內(nèi)容,不然獲取的就是空。
2016-11-16
但是下面的代碼就可以,為什么啊?
window.onload = function() {
?? ?var h = document.getElementsByTagName("tr");
?? ?for(var i = 1; i < h.length; i++) {change(h[i]);}}
?? ?function change(obj) {
?? ?obj.onmousemove = function() {obj.style.backgroundColor = "#f2f2f2";}
?? ?obj.onmouseout = function() {obj.style.backgroundColor = "#fff";}}