不找哪行的問題,添加不了行
</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="#" onclick = "deleteItem(this);return false;" >刪除</a></td>
? ? ?</tr>
? ? ?<tr>
? ? <td>xh002</td>
? ? <td>劉小芳</td>
? ? <td><a href="javasript:;" onclick = "deleteItem(this);" >刪除</a></td>
? ? ?</tr>
? ? ?</table>
? ? ?<input type="button" value="添加一行" ?onclick = "addTo();"/>
?</body>
</html>
2018-07-05
代碼沒有太大問題,都是些細(xì)節(jié)問題,不難找出錯(cuò)誤。錯(cuò)誤一: td.innerHTML='<a href="#" onclick = "deleteItem(this);return false;">刪除</a>;這行代碼改為:td.innerHTML="<a href='#' onclick='deleteItem(this);return false;'>刪除</a>";。這里面的錯(cuò)誤就是雙引號(hào)和單引號(hào)的用法,我就不多做介紹了。錯(cuò)誤二:table.removeChild(tr);改成:table.removeChild(tr);這里面的錯(cuò)誤就是中文分號(hào)和英文分號(hào)的問題,打字后記得切換為英文輸入法,這是一個(gè)程序員的基本素質(zhì),不然出現(xiàn)小的細(xì)節(jié)錯(cuò)誤很難發(fā)現(xiàn)。
2018-06-20
<!DOCTYPE html>
<html>
?<head>
? <title> new document </title> ?
? <meta http-equiv="Content-Type" content="text/html; charset=gbk"/> ?
? <script type="text/javascript">
? ? ? window.onload = function(){
? ? ? ? ? ?var trs = document.getElementsByTagName('tr');
? ? ? ? ? ?for(var i = 1; i < trs.length; i++)
? ? ? ? ? ?{
? ? ? ? ? ? ? ? trs[i].onmouseover = function()//觸發(fā)trs【i】的鼠標(biāo)移到上面事件
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ?this.style.backgroundColor = "#f2f2f2";//鼠標(biāo)移到上面觸發(fā)背景色改變
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? trs[i].onmouseout = function()
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ?this.style.backgroundColor = "#fff";
? ? ? ? ? ? ? ? }
? ? ? ? ? ?}
? ? ? ? }
? ? ?function addTo(){
? ? ? ? ? ?var table = document.getElementById('table').lastChild;//訪問table節(jié)點(diǎn)下的最后一個(gè)子節(jié)點(diǎn)
? ? ? ? ? ?var tr = document.createElement('tr');//創(chuàng)建元素節(jié)點(diǎn)
? ? ? ? ? ?var td = document.createElement('td');//創(chuàng)建元素節(jié)點(diǎn)
? ? ? ? ? ?td.innerHTML="<input type = 'text' />";
? ? ? ? ? ?tr.appendChild(td);
? ? ? ? ? ?td = document.createElement('td');
? ? ? ? ? ?td.innerHTML="<input type = 'text' />";
? ? ? ? ? ?tr.appendChild(td);//在指定節(jié)點(diǎn)的最后一個(gè)子節(jié)點(diǎn)后添加一個(gè)子節(jié)點(diǎn)
? ? ? ? ? ?td = document.createElement('td');
? ? ? ? ? ?td.innerHTML='<a href="#" onclick = "deleteItem(this);return false;">刪除</a>;
? ? ? ? ? ?tr.appendChild(td);
? ? ? ? ? ?table.appendChild(tr);
? ? ? ? ? }
? ? ? function deleteItem(obj){
? ? ? ? ? ? var table = document.getElementById('table').lastChild;
? ? ? ? ? ? var tr = obj.parentNode.parentNode;
? ? ? ? ? ? table.removeChild(tr);
? ? ? }
? </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="#" onclick = "deleteItem(this);return false;">刪除</a></td> ? <!--在刪除按鈕上添加點(diǎn)擊事件 ?-->
? </tr>
? <tr>
<td>xh002</td>
<td>劉小芳</td>
<td >
? ?<a href="javascript:;" onclick="deleteItem(this);" ?>刪除</a></td> ? <!--在刪除按鈕上添加點(diǎn)擊事件 ?-->
? </tr> ?
? </table>
? <input type="button" value="添加一行" onclick="addTo();" /> ? <!--在添加按鈕上添加點(diǎn)擊事件 ?-->
?</body>
</html>
2018-06-20
js代碼貼一下