今天在慕课网学习完成后的一个综合性编程里遇到了一个问题。是鼠标移到表格不同行上时背景色改为色值为 #f2f2f2,移开鼠标时则恢复为原背景色 #fff时遍历循环后发生了style 未定义的问题。
以下贴出错误代码:
<!DOCTYPE html>
<html>
<head>
<title> new document </title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<script type="text/javascript">
window.onload = function(){
// 鼠标移动改变背景,可以通过给每行绑定鼠标移上事件和鼠标移除事件来改变所在行背景色。
var tableRows=document.getElementsByTagName("tr");
for(var i=1;i<tableRows.length;i++){
tableRows[i].onmouseover=function(){
tableRows[i].style.backgroundColor="yellow";
}
tableRows[i].onmouseout=function(){
tableRows[i].style.backgroundColor="#CCC";
}
}
}
// 编写一个函数,供添加按钮调用,动态在表格的最后一行添加子节点;
function addRow(){
var tabl=document.getElementById("table");
var name=prompt("请输入姓名:");
if(name!=null){
var num=tabl.childNodes.length+1;
var newRow=document.createElement("tr");
var childTd1=document.createElement("td");
var childTd2=document.createElement("td");
var childTd3=document.createElement("td");
childTd1.innerHTML="xh00"+num;
childTd2.innerHTML=name;
childTd3.innerHTML="<a href='javascript:;' onclick='delRow(this)' >删除</a>";
newRow.appendChild(childTd1);
newRow.appendChild(childTd2);
newRow.appendChild(childTd3);
changeColor(newRow);
tabl.appendChild(newRow);
}
}
// 创建删除函数
function delRow(obj){
var father=obj.parentNode.parentNode;
father.parentNode.removeChild(father);
}
</script>
</head>
<body>
<table border="1" width="50%" id="table">
<tr>
<th>学号</th>
<th>姓名</th>
<th>操作</th>
</tr>
<tr >
<td>xh001</td>
<td>王小明</td>
<td><a href="javascript:;" onclick="delRow(this)" >删除</a></td> <!--在删除按钮上添加点击事件 this指当前标签本身 -->
</tr>
<tr >
<td>xh002</td>
<td>刘小芳</td>
<td><a href="javascript:;" onclick="delRow(this)" >删除</a></td> <!--在删除按钮上添加点击事件 -->
</tr>
</table>
<input type="button" value="添加一行" onclick="addRow()" /> <!--在添加按钮上添加点击事件 -->
</body>
</html>
代码在处理鼠标移动改变背景这里时出现了问题,原因是
当循环完成后,onmouseover已经指定调用function()这个函数。
此时i为3,而再将鼠标放tr标签上时就会进入function,调用tableRows[3].style.backgroundColor="yellow";此时找不到第三行就会报style未定义的错。
改正方法:
或
最后代码:
<!DOCTYPE html>
<html>
<head>
<title> new document </title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<script type="text/javascript">
window.onload = function(){
// 鼠标移动改变背景,可以通过给每行绑定鼠标移上事件和鼠标移除事件来改变所在行背景色。
var tableRows=document.getElementsByTagName("tr");
for(var i=1;i<tableRows.length;i++){
//错误写法,因为当循环完成后,onmouseover已经指定调用function()这个函数。
//此时i为3,而再将鼠标放tr标签上时就会进入function,调用tableRows[3].style.backgroundColor="yellow";此时找不到第三行就会报style未定义的错。
// tableRows[i].onmouseover=function(){
// tableRows[i].style.backgroundColor="yellow";
// }
// tableRows[i].onmouseout=function(){
// tableRows[i].style.backgroundColor="#CCC";
// }
//正确写法是封装在函数里 或者用this来调用
// tableRows[i].onmouseover=function(){
// this.style.backgroundColor="yellow";
// }
// tableRows[i].onmouseout=function(){
// this.style.backgroundColor="#CCC";
// }
//正确方法二
changeColor(tableRows[i]);
}
}
function changeColor(obj){
obj.onmouseover=function(){
obj.style.backgroundColor="yellow";
}
obj.onmouseout=function(){
obj.style.backgroundColor="#CCC";
}
}
// 编写一个函数,供添加按钮调用,动态在表格的最后一行添加子节点;
function addRow(){
var tabl=document.getElementById("table");
var name=prompt("请输入姓名:");
if(name!=null){
var num=tabl.childNodes.length+1;
var newRow=document.createElement("tr");
var childTd1=document.createElement("td");
var childTd2=document.createElement("td");
var childTd3=document.createElement("td");
childTd1.innerHTML="xh00"+num;
childTd2.innerHTML=name;
childTd3.innerHTML="<a href='javascript:;' onclick='delRow(this)' >删除</a>";
newRow.appendChild(childTd1);
newRow.appendChild(childTd2);
newRow.appendChild(childTd3);
changeColor(newRow);
tabl.appendChild(newRow);
}
}
// 创建删除函数
function delRow(obj){
var father=obj.parentNode.parentNode;
father.parentNode.removeChild(father);
}
</script>
</head>
<body>
<table border="1" width="50%" id="table">
<tr>
<th>学号</th>
<th>姓名</th>
<th>操作</th>
</tr>
<tr >
<td>xh001</td>
<td>王小明</td>
<td><a href="javascript:;" onclick="delRow(this)" >删除</a></td> <!--在删除按钮上添加点击事件 this指当前标签本身 -->
</tr>
<tr >
<td>xh002</td>
<td>刘小芳</td>
<td><a href="javascript:;" onclick="delRow(this)" >删除</a></td> <!--在删除按钮上添加点击事件 -->
</tr>
</table>
<input type="button" value="添加一行" onclick="addRow()" /> <!--在添加按钮上添加点击事件 -->
</body>
</html>