求教哪里錯(cuò)了,看了兩小時(shí)實(shí)在看不出來(lái)……
<!DOCTYPE html>
<html>
?<head>
? <title> new document </title> ?
? <meta http-equiv="Content-Type" content="text/html; charset=gbk"/>? ?
? <script type="text/javascript">
?
??? // 鼠標(biāo)移動(dòng)改變背景,可以通過(guò)給每行綁定鼠標(biāo)移上事件和鼠標(biāo)移除事件來(lái)改變所在行背景.
? ?
??? window.onload = function()
??? {
????? Highlight();
?? ?}
?? ?
?? ?function Highlight()
?? ?{
?? ???? var row=document.getElementsByTagName("tr");
??????? for(i=0;i<row.length;i++)
??????? {
??????????? !function(i){
??????????????? row[i].onmouseover=function()
??????????????? {
??????????????????? row[i].style.backgroundColor="#CCC";
??????????????? }
??????????????? row[i].onmouseout=function()
??????????????? {
??????????????????? row[i].style.backgroundColor="#fff";
??????????????? }
??????????? }(i);
??????? }
?? ?}
?? ?
????? // 編寫一個(gè)函數(shù),供添加按鈕調(diào)用,動(dòng)態(tài)在表格的最后一行添加子節(jié)點(diǎn);
??? function add()
??? {
??????? var newtr=document.createElement("tr");
?????? ?
??????? var newtd1=document.createElement("td");
??????? newtd1.innerHTML="<input type='text'/>";
?????? ?
??????? var newtd2=document.createElement("td");
??????? newtd2.innerHTML="<input type='text'/>";
?????? ?
??????? var newtd3=document.createElement("td");
??????? newtd3.innerHTML="<a href='javasript:;' onclick='delete(this)'>刪除<a/>";
?????? ?
??????? var body=document.getElementById("table");
?????? ?
??????? newtr.appendChild(newtd1);
??????? newtr.appendChild(newtd2);
??????? newtr.appendChild(newtd3);
??????? body.appendChild(newtr);
?????? ?
??????? Highlight();
??? }
?? ??? ??? ?
? ??? ?
??? // 創(chuàng)建刪除函數(shù)
??? function delete(obj)
??? {
???????? var tr=obj.parentNode.parentNode;
???????? tr.parentNode.removeChild(tr);
??? }
?? ?
??? /*
??? function delete1()
??? {
??????? var item1=document.getElementById("item1");
??????? var child1=item1.parentNode.childNodes;
??????? for(var i=(child1.length-1);i<child1.length;i--)
??????? {
??????????? if(child1[i].id="item1")
??????????? {
??????????????? var childtd1=item1.childNodes;
??????????????? for(var j=(childtd1.length-1);j<childtd1.length;j--)
??????????????? {
??????????????????? item1.removeChild(childtd1[j]);
??????????????? }
??????????????? item1.paremtNode.removeChild(child1[i]);
??????????? }
??????? }
??? }
??? function delete2()
???? {
??????? var item2=document.getElementById("item2");
??????? var child2=item2.parentNode.childNodes;
??????? for(var i=(child2.length-1);i<child2.length;i--)
??????? {
??????????? if(child2[i].id="item2")
??????????? {
??????????????? var childtd2=item2.childNodes;
??????????????? for(var j=(childtd2.length-1);j<childtd2.length;j--)
??????????????? {
??????????????????? item2.removeChild(childtd2[j]);
??????????????? }
??????????????? item2.paremtNode.removeChild(child2[i]);
??????????? }
??????? }
??? }
??? */
? </script>
?</head>
?<body>
?? ??? <table border="1" width="50%" id="table">
?? ??? <tr>
?? ??? ?<th>學(xué)號(hào)</th>
?? ??? ?<th>姓名</th>
?? ??? ?<th>操作</th>
?? ??? </tr> ?
?? ??? <tr>
?? ??? ?<td>xh001</td>
?? ??? ?<td>王小明</td>
?? ??? ?<td><a href="javascript:;" onclick="delete(this)">刪除</a></td>? ?
?? ??? ?<!--在刪除按鈕上添加點(diǎn)擊事件? -->
?? ??? </tr>
?? ??? <tr>
?? ??? ?<td>xh002</td>
?? ??? ?<td>劉小芳</td>
?? ??? ?<td><a href="javascript:;" onclick="delete(this)">刪除</a></td>? ?
?? ??? ?<!--在刪除按鈕上添加點(diǎn)擊事件? -->
?? ??? </tr> ?
?? ??? </table>
?? ??? <input type="button" value="添加一行" onclick="add()"? />? ?
?? ??? <!--在添加按鈕上添加點(diǎn)擊事件? -->
?</body>
</html>
2018-06-13
你的函數(shù)名delete是JS的預(yù)留關(guān)鍵字