JavaScript相关
<!DOCTYPE html> <html> <head> <title>new document</title> <meta http-equiv="Content-Type" content="text/html; charset=gbk"/> <script type="text/javascript"> window.onload=gaoliang(); //高亮 function gaoliang(){ var a=document.getElementById('table').lastChild; var trs=document.getElementsByTagName('tr'); for(var i=1;i<trs.length;i++){ trs[i].onmouseover=function(){ this.style.backgroundColor="red"; } trs[i].onmouseout=function(){ this.style.backgroundColor="blue"; } } } //添加按钮 function tianjia(){ var a=document.getElementById("table").lastChild; var tr=document.createElement("tr"); var td=document.createElement("td"); td.innerHTML="<input type='text' />"; tr.appendChild(td); td=document.createElement("td"); td.innerHTML="<input type='text' />"; tr.appendChild(td); td=document.createElement("td"); td.innerHTML="<a href='#' onClick='shanchu(this)'>删除</a>"; //不能用双引号 用单引号 tr.appendChild(td); a.appendChild(tr); gaoliang(); } //删除按钮 function shanchu(obj){ var a=obj.parentNode.parentNode; document.getElementById("table").lastChild.removeChild(a); } </script> </head> <body> <table border="1px solid black" width="50%" id="table"> <tr> <th>学号</th> <th>姓名</th> <th>操作</th> </tr> <tr> <td>001</td> <td>小明</td> <td><a href="#" onClick="shanchu(this)">删除</a></td> </tr> <tr> <td>002</td> <td>小芳</td> <td><a href="#" onClick="shanchu(this)">删除</a></td> </tr> </table> <input type="button" value="添加" onClick="tianjia()"> </body> </html>
为什么我在window.onload调用函数不行啊