水文刀
2016-05-05 14:51
这是我自己的代码语言实现鼠标变色功能,期间也参考了正解和他人的代码都没有看到变色效果,是代码问题吗?还是浏览器问题?
function onload() {
var tbody = document.getElementById('table').lastChild;
var trlist = tbody.getElementsByTagName('tr');
for (var i = 1; i < trlist.length; i++) {
trlist[i].onmouseover = function () {
this.style.backgroundColor = "#f2f2f2";
}
trlist[i].onmouseout = function () {
this.style.backgroundColor = "#fff";
}
}
}
你这只是定义了一个名为onload的函数。window.onload = function(){}是页面加载完后将执行这里的匿名函数。
i=0,应该是从0开始的,如果前面有window.onload的话,把function onload去掉试试,试了一下是可以的。
window.onload = function(){
// 鼠标移动改变背景,可以通过给每行绑定鼠标移上事件和鼠标移除事件来改变所在行背景色。
var tdarr = document.getElementsByTagName("tr");
for (var i=0; i< tdarr.length;i++){
tdarr[i].onmouseover = function(){
this.style.backgroundColor ="#f2f2f2";
}
tdarr[i].onmouseout = function(){
this.style.backgroundColor ="#fff";
}
}
}
这事我的最后一个练习题的代码,你看下吧
JavaScript进阶篇
469067 学习 · 22582 问题
相似问题