課程
/前端開發(fā)
/JavaScript
/JavaScript進階篇
我是按照參考答案寫的,自己寫不出來,很遺憾!即便是這樣,我發(fā)現(xiàn)還是有錯誤。請幫幫我找出來(我花費了很長時間了)
2016-12-13
源自:JavaScript進階篇 9-22
正在回答
樓上說的對,12行
var tbody = document.getElementById('tbody').lastChild;
注意是
getElementById('table')
代碼的第12行,你并沒有id=“tbody”的標簽呀!我用的也是火狐的firebug,在火狐菜單-->附加組件-->插件 然后搜索firebug安裝就可以了。我也是小白,希望可以幫到你!
火狐 firbug
用瀏覽器調(diào)試 他會告訴你錯誤在哪里
慕粉4340077 提問者
<!DOCTYPE html><html>?<head>? <title> new document </title> ?? <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>? ?? <script type="text/javascript"> ? ?? ????? hover(); ???? // 鼠標移動改變背景,可以通過給每行綁定鼠標移上事件和鼠標移除事件來改變所在行背景色。???? function hover(){???????? var tab = document.getElementsByTagName("table")[0];?? ??? ? var tabtr = document.getElementsByTagName("tr");??? ??? ?for (i=0;i<tabtr.length;i++) {??? ??? ??? ?tabtr[i].onmouseenter = function(){??? ??? ??? ??? ?this.style.backgroundColor="#f2f2f2";??? ??? ??? ?}??? ??? ??? ?tabtr[i].onmouseleave = function(){??? ??? ??? ??? ?this.style.backgroundColor="#fff";??? ??? ??? ?}??? ??? ?}???? }??? ??? ? ?? ? ???? var index;????? // 編寫一個函數(shù),供添加按鈕調(diào)用,動態(tài)在表格的最后一行添加子節(jié)點;???? function add(){??? ??? ?index=0;??? ??? ?var tab = document.getElementsByTagName("table")[0];??? ??? ?var tr = document.createElement("tr");??? ??? ?var td1 = document.createElement("td");??? ??? ?td1.innerHTML="xh001";??? ??? ?tr.appendChild(td1);??? ??? ?var td2 = document.createElement("td");??? ??? ?td2.innerHTML="王小明";??? ??? ?tr.appendChild(td2);??? ??? ?var a= document.createElement("a");??? ??? ?var td3 = document.createElement("td");??? ??? ?a.innerHTML="刪除";??? ??? ?a.setAttribute("href","javascript:;");?? ?? ??? ?td3.appendChild(a);??? ??? ?tr.appendChild(td3);??? ??? ?tab.appendChild(tr);??? ??? ?a.setAttribute("onclick","remove(this)");??? ??? ?hover();???? }?? ??? ??? ?? ??? ? ???? // 創(chuàng)建刪除函數(shù)???? function remove(obj){??? ??? ?var tab = obj.parentNode.parentNode.parentNode;??? ??? ?var tr = obj.parentNode.parentNode;??? ??? ?tab.removeChild(tr)??? ??? ?hover();??? ??? ?return false;???? }? </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:;" onclick="remove(this)">刪除</a></td>?? <!--在刪除按鈕上添加點擊事件? -->?? ??? </tr>?? ??? <tr>?? ??? ?<td>xh002</td>?? ??? ?<td>劉小芳</td>?? ??? ?<td><a href="javascript:;" onclick="remove(this)">刪除</a></td>?? <!--在刪除按鈕上添加點擊事件? -->?? ??? </tr> ??? ??? </table>?? ??? <input type="button" value="添加一行"? onclick="add()"/>?? <!--在添加按鈕上添加點擊事件? -->?</body></html>
舉報
本課程從如何插入JS代碼開始,帶您進入網(wǎng)頁動態(tài)交互世界
1 回答javascript進階篇9-22編程練習
1 回答JavaScript進階篇9-22 編程練習
3 回答JavaScript進階篇 9-22編程練習 問題求助?
1 回答進階篇的9-22的編程練習的程序
4 回答javascript進階篇 4-10 編程練習
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網(wǎng)安備11010802030151號
購課補貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動學習伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號
2017-01-03
樓上說的對,12行
var tbody = document.getElementById('tbody').lastChild;
注意是
getElementById('table')
2016-12-17
代碼的第12行,你并沒有id=“tbody”的標簽呀!我用的也是火狐的firebug,在火狐菜單-->附加組件-->插件 然后搜索firebug安裝就可以了。我也是小白,希望可以幫到你!
2016-12-14
火狐
firbug
2016-12-13
用瀏覽器調(diào)試 他會告訴你錯誤在哪里
2016-12-13
<!DOCTYPE html>
<html>
?<head>
? <title> new document </title> ?
? <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>? ?
? <script type="text/javascript">
?
?? ?
???? hover();
???? // 鼠標移動改變背景,可以通過給每行綁定鼠標移上事件和鼠標移除事件來改變所在行背景色。
???? function hover(){
???????? var tab = document.getElementsByTagName("table")[0];
?? ??? ? var tabtr = document.getElementsByTagName("tr");
??? ??? ?for (i=0;i<tabtr.length;i++) {
??? ??? ??? ?tabtr[i].onmouseenter = function(){
??? ??? ??? ??? ?this.style.backgroundColor="#f2f2f2";
??? ??? ??? ?}
??? ??? ??? ?tabtr[i].onmouseleave = function(){
??? ??? ??? ??? ?this.style.backgroundColor="#fff";
??? ??? ??? ?}
??? ??? ?}
???? }
??? ?
?? ?
?? ?
???? var index;
????? // 編寫一個函數(shù),供添加按鈕調(diào)用,動態(tài)在表格的最后一行添加子節(jié)點;
???? function add(){
??? ??? ?index=0;
??? ??? ?var tab = document.getElementsByTagName("table")[0];
??? ??? ?var tr = document.createElement("tr");
??? ??? ?var td1 = document.createElement("td");
??? ??? ?td1.innerHTML="xh001";
??? ??? ?tr.appendChild(td1);
??? ??? ?var td2 = document.createElement("td");
??? ??? ?td2.innerHTML="王小明";
??? ??? ?tr.appendChild(td2);
??? ??? ?var a= document.createElement("a");
??? ??? ?var td3 = document.createElement("td");
??? ??? ?a.innerHTML="刪除";
??? ??? ?a.setAttribute("href","javascript:;");
?? ?? ??? ?td3.appendChild(a);
??? ??? ?tr.appendChild(td3);
??? ??? ?tab.appendChild(tr);
??? ??? ?a.setAttribute("onclick","remove(this)");
??? ??? ?hover();
???? }
?? ??? ??? ?
? ??? ?
???? // 創(chuàng)建刪除函數(shù)
???? function remove(obj){
??? ??? ?var tab = obj.parentNode.parentNode.parentNode;
??? ??? ?var tr = obj.parentNode.parentNode;
??? ??? ?tab.removeChild(tr)
??? ??? ?hover();
??? ??? ?return false;
???? }
? </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:;" onclick="remove(this)">刪除</a></td>?? <!--在刪除按鈕上添加點擊事件? -->
?? ??? </tr>
?? ??? <tr>
?? ??? ?<td>xh002</td>
?? ??? ?<td>劉小芳</td>
?? ??? ?<td><a href="javascript:;" onclick="remove(this)">刪除</a></td>?? <!--在刪除按鈕上添加點擊事件? -->
?? ??? </tr> ?
?? ??? </table>
?? ??? <input type="button" value="添加一行"? onclick="add()"/>?? <!--在添加按鈕上添加點擊事件? -->
?</body>
</html>