<!DOCTYPE html> <html> <head> <title> new document </title> <meta http-equiv="Content-Type" content="text/html; charset=gbk"/> <style type="text/css"> .show{ background-color:#9F6; color:#00C; } </style> <script type="text/javascript"> window.onload = function(){ // !! 功能不能实现 鼠标移动改变背景,可以通过给每行绑定鼠标移上事件和鼠标移除事件来改变所在行背景色。 var rowList = document.getElementsByTagName("tr"); for(var i=1;i<rowList.length;i++){ rowList[i].onmouseover = function(){ rowList[i].className = "show"; } rowList[i].onmouseout = function(){ rowList[i].className = "hideShow"; } } } // 编写一个函数,供添加按钮调用,动态在表格的最后一行添加子节点; function add(){ var content = prompt("请输入您的学号和姓名,中间以逗号隔开",""); var id = content.split(",")[0]; var name = content.split(",")[1]; var row = document.createElement("tr"); var tdName = document.createElement("td"); var tdId = document.createElement("td"); var tdOpe = document.createElement("td"); var link = document.createElement("a"); tdName.innerHTML = name; tdId.innerHTML = id; link.innerHTML = "删除"; link.setAttribute("href","javascript:del();"); row.appendChild(tdId); row.appendChild(tdName); tdOpe.appendChild(link); row.appendChild(tdOpe); var table = document.getElementById("table"); table.appendChild(row); } // !!功能不能实现 创建删除函数 function del(){ var thisRow = this.parentNode; var table = document.getElementById("table"); table.removeChild(thisRow); } </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:del();" >删除</a></td> <!--在删除按钮上添加点击事件 --> </tr> <tr> <td>xh002</td> <td>刘小芳</td> <td><a href="javascript:del();" >删除</a></td> <!--在删除按钮上添加点击事件 --> </tr> </table> <input type="button" value="添加一行" onclick="add()" /> <!--在添加按钮上添加点击事件 --> </body> </html>
黑女2008
Siming0