<!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'); for (var i = 0; i < trs.length; i++){ setOver(trs[i]); setOut(trs[i]); }; } //背景色函数 function setOver (obj) { obj.onmouseover = function(){ this.style.backgroundColor = "##f2f2f2" }; } function setOut (obj) { obj.onmouseout = function(){ this.style.backgroundColor = "#fff" };; } //删除函数 function removeLine (obj) { var oG = obj.parentNode.parentNode.parentNode; var oP = obj.parentNode.parentNode; oG.removeChild(oP); } //添加函数 function newLine () { var stuName = prompt("enter name",""); var stuId = prompt("enter number",""); var objTr = document.createElement('tr'); objTr.innerHTML = "<td>"+stuId+"</ td><td>"+stuName+"</ td><td><a href = 'javascript:;' onclick = 'removeLine(this)' >删除</ a></ td> "; setOver(objTr); setOut(objTr); document.getElementById('table').lastChild.appendChild(objTr); } </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 = "removeLine(this)">删除</a></td> <!--在删除按钮上添加点击事件 --> </tr> <tr> <td>xh002</td> <td>刘小芳</td> <td><a href="javascript:;" onclick = "removeLine(this)">删除</a></td> <!--在删除按钮上添加点击事件 --> </tr> </table> <input type="button" value="添加一行" onclick="newLine()" /> <!--在添加按钮上添加点击事件 --> </body> </html>
因为想要给新增项上加颜色,所以 声明了一个setOver setOut 函数,想要在创建新行的时候调用。但是就是无动于衷啊。急死我了,求同学们指导啊!
猗顿
奋斗小青年111