<html> <head> <title> new documents </title> <meta http-equiv='content-Type' content = 'text/html; charset =utf-8' /> <script type ='text/javascript'> function addOne(obj){ var tbody = documents.getElemenById('table').lastChild; var tr = document.createElement('tr'); var td = document.createElement('td'); td.innerHTML = "<input type = 'text'/>"; tr.appenChild(td); td = document.createElement('td'); td.innerHTML="<input type='text'/>"; tr.appendChild(td); td = document.createElement('td'); td.innerHTML= "<a href = 'javascript:;' onclick = 'deleteRow(this)'>删除</a>"; tr.appenChild(td); tbody.appenChild(tr); Highlight(); } function deleteRow(obj){ var tbody = document.getElemenById('table').lastChild; var tr = obj.parentNode.parentNode; tbody.removeChild(tr); } function Highlight(){ var body= document.getElemenById('table').lastChild; trs = tbody.getElemenByTagName('tr') for (var i =1;i<trs.length;i++){ trs[i].onmouseover=function(){ this.style.backgroundColor='#c00'; } trs[i].onmouseout =function(){ this.style.backgroundColor='#fff'; } } } </script> </head> <body> <table border= '1' width ='50%' id ="table" onmouseover='Highlight()'> <tr> <th>学号</th> <th>姓名</th> <th>操作</th> </tr> <tr> <td>xh001</td> <td>王小明</td> <td><a href = 'javascript:;' onclick="deleteRow(this)"> 删除</a><td> </tr> <tr> <td>xh002</td> <td>刘小芳</td> <td><a href= 'javascript:;' onclick ="deleteRow(this)">删除</a></td> </tr> </table> <input type='button' value="添加一行" onclick = "addOne()"/> </body> </html>
说实话 ,你这代码的错误貌似有点多。
大略看一下,错误有俩个:
一、document.getElemenById('table') Element 拼写错误,并且错了很多次。
二、window.onload=function(){Highlight()} 没写。
不止这些错误。