<!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 trs=document.getElementsByTagName("tr");
console.log(trs);
for(var i=0;i<trs.length;i++){
console.log(i);
trs[i].setAttribute("onmouseover","msover(this)");
trs[i].setAttribute("onmouseout","msout(this)");
}
var as=document.getElementsByTagName("a");
for(var i=0;i<as.length;i++){
if (as[i].innerHTML="删除"){
as[i].setAttribute("onclick","this.parentNode.parentNode.parentNode.removeChild(this.parentNode.parentNode)");
}
}
var newlines = document.getElementsByTagName("input");
for(var i=0;i<newlines.length;i++){
if (newlines[i].value="添加一行"){
newlines[i].setAttribute("onclick","addline()")
}
}
}
function addline(){
var newl = document.createElement("tr");
newl.appendChild(document.createElement("td"));
newl.appendChild(document.createElement("td"));
var tda = newl.appendChild(document.createElement("td"));
newa= document.createElement("a");
newa.href="javascript:void(0);";
newa.innerHTML="delrow";
newa.setAttribute("onclick","this.parentNode.parentNode.remove();");
tda.appendChild(newa);
document.getElementsByTagName("tbody")[0].appendChild(newl);
}
// 编写一个函数,供添加按钮调用,动态在表格的最后一行添加子节点;
// 创建删除函数
</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:;" >删除</a></td> <!--在删除按钮上添加点击事件 -->
</tr>
<tr>
<td>xh002</td>
<td>刘小芳</td>
<td><a href="javascript:;" >删除</a></td> <!--在删除按钮上添加点击事件 -->
</tr>
</table>
<input type="button" value="添加一行" /> <!--在添加按钮上添加点击事件 -->
<script type="text/javascript">
function msover(x){
x.style.backgroundColor = "#ffffff";
}
function msout(x){
x.style.backgroundColor="#F2F2F2";
}
</script>
</body>
</html>为啥我这里的 msover() msout() 方法必须写在 body 里才能运行,而写在 head里的window.onload里就无法运行呢?
stone310
qq_猫须_0
相关分类