<!DOCTYPE html><html> <head> <title> new document </title> <meta http-equiv="Content-Type" content="text/html; charset=gbk"/> <script type="text/javascript"> window.onload = function(){ // 鼠标移动改变背景,可以通过给每行绑定鼠标移上事件和鼠标移除事件来改变所在行背景色。 var td=document.getElementsByTagName("tr"); for(var i=0;i<tr.length;i++){ tr[i].onmouseover=function(){ this.style.backgroundColor="#f2f2f2"; } tr[i].onmouseout=function(){ this.style.backgroundColor="#fff"; } } } // 编写一个函数,供添加按钮调用,动态在表格的最后一行添加子节点; function add(){ var a1=document.createElement("td"); var a2=document.createElement("td"); var a3=document.createElement("td"); var aa=document.createElement("a"); var a=document.createElement("tr"); var table=document.getElementsByTagName("table")[0]; a1.innerHTML="xh00"+parseInt(table.childNodes.length+1); a2.innerHTML=table.childNodes.length+1+"号"; aa.innerHTML="<a href='javascript:;' onclick='delet(this)' >删除</a>"; a3.appendChild(aa); a.appendChild(a1); a.appendChild(a2); a.appendChild(a3); table.appendChild(a); //alert("table"); var tr=document.getElementsByTagName("tr"); for(var i=0;i<tr.length;i++){ tr[i].onmouseover=function(){ this.style.backgroundColor="#f2f2f2"; } tr[i].onmouseout=function(){ this.style.backgroundColor="#fff"; } } } // 创建删除函数 function delet(obj){ var table=document.getElementById("table").lastChild; var tr =obj.parentNode.parentNode; table.removeChild(tr); } </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="delet(this);">删除</a></td> <!--在删除按钮上添加点击事件 --> </tr> <tr> <td>xh002</td> <td>刘小芳</td> <td><a href="javascript:" onclick="delet(this);">删除</a></td> <!--在删除按钮上添加点击事件 --> </tr> </table> <input type="button" value="添加一行" onclick="add()" /> <!--在添加按钮上添加点击事件 --> </body></html>
1 回答
已采纳
stone310
TA贡献361条经验 获得超191个赞
把你原来的注释删了,新的注释都是改动了的地方
<script type="text/javascript"> window.onload = function () { var tr = document.getElementsByTagName("tr"); //获取tr var td = document.getElementsByTagName("tr"); for (var i = 0; i < tr.length; i++) { tr[i].onmouseover = function () { this.style.backgroundColor = "#f2f2f2"; } tr[i].onmouseout = function () { this.style.backgroundColor = "#fff"; } } } function add() { var a1 = document.createElement("td"); var a2 = document.createElement("td"); var a3 = document.createElement("td"); var aa = document.createElement("a"); var a = document.createElement("tr"); var table = document.getElementsByTagName("table")[0]; a1.innerHTML = "xh00" + parseInt(table.childNodes.length + 1); a2.innerHTML = table.childNodes.length + 1 + "号"; // aa.innerHTML="<a href='javascript:;' onclick='delet(this)' >删除</a>"; a3.innerHTML = "<a href='javascript:;' onclick='delet(this)' >删除</a>"; //放在td下 // a3.appendChild(aa); a.appendChild(a1); a.appendChild(a2); a.appendChild(a3); // table.appendChild(a); table.lastChild.appendChild(a); //整个新增tr放在tbody下 var tr = document.getElementsByTagName("tr"); for (var i = 0; i < tr.length; i++) { tr[i].onmouseover = function () { this.style.backgroundColor = "#f2f2f2"; } tr[i].onmouseout = function () { this.style.backgroundColor = "#fff"; } } } function delet(obj) { var table = document.getElementById("table").lastChild; var tr = obj.parentNode.parentNode; table.removeChild(tr); } </script>
添加回答
举报
0/150
提交
取消