課程
/前端開(kāi)發(fā)
/JavaScript
/JavaScript進(jìn)階篇
...
2016-06-30
源自:JavaScript進(jìn)階篇 9-22
正在回答
<!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(){???????????????? ????? // 鼠標(biāo)移動(dòng)改變背景,可以通過(guò)給每行綁定鼠標(biāo)移上事件和鼠標(biāo)移除事件來(lái)改變所在行背景色??? ????????? var trs = document.getElementsByTagName('tr');?????????? for(var i = 1; i < trs.length; i++){??????????????? trs[i].onmouseover = function(){???????????????????? this.style.backgroundColor = "#333";??????????????? }??????????????? trs[i].onmouseout = function(){???????????????????? this.style.backgroundColor = "#fff";??????????????? }????????? }?? ??? ???? ??? ? ?? ? }??? ?????? // 編寫(xiě)一個(gè)函數(shù),供添加按鈕調(diào)用,動(dòng)態(tài)在表格的最后一行添加子節(jié)點(diǎn);???? function addTo(){?????????? var table = 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 = "deleteItem(this); return false;" >刪除</a>';?????????? tr.appendChild(td);?????????? table.appendChild(tr);????????? }?? ??? ??? ?? ??? ? ???? // 創(chuàng)建刪除函數(shù)???? 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 onclick = "deleteItem(this);return false;" >刪除</a></td>?? <!--在刪除按鈕上添加點(diǎn)擊事件? -->?? ??? </tr>?? ??? <tr>?? ??? ?<td>xh002</td>?? ??? ?<td>劉小芳</td>?? ??? ?<td><a? onclick = "deleteItem(this);" >刪除</a></td>?? <!--在刪除按鈕上添加點(diǎn)擊事件? -->?? ??? </tr> ??? ??? </table>?? ??? <input type="button" value="添加一行"? onclick = "addTo();"/>?? <!--在添加按鈕上添加點(diǎn)擊事件? -->?</body></html>
舉報(bào)
本課程從如何插入JS代碼開(kāi)始,帶您進(jìn)入網(wǎng)頁(yè)動(dòng)態(tài)交互世界
1 回答已解決
1 回答解決了
2 回答怎么解決?
1 回答問(wèn)題解決了
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號(hào)-11 京公網(wǎng)安備11010802030151號(hào)
購(gòu)課補(bǔ)貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動(dòng)學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號(hào)
2016-06-30
<!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(){
???????????????? ?
???? // 鼠標(biāo)移動(dòng)改變背景,可以通過(guò)給每行綁定鼠標(biāo)移上事件和鼠標(biāo)移除事件來(lái)改變所在行背景色
??? ?
???????? var trs = document.getElementsByTagName('tr');
?????????? for(var i = 1; i < trs.length; i++){
??????????????? trs[i].onmouseover = function(){
???????????????????? this.style.backgroundColor = "#333";
??????????????? }
??????????????? trs[i].onmouseout = function(){
???????????????????? this.style.backgroundColor = "#fff";
??????????????? }
????????? }
?? ??? ?
??? ?
?? ?
?? ? }
??? ?
????? // 編寫(xiě)一個(gè)函數(shù),供添加按鈕調(diào)用,動(dòng)態(tài)在表格的最后一行添加子節(jié)點(diǎn);
???? function addTo(){
?????????? var table = 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 = "deleteItem(this); return false;" >刪除</a>';
?????????? tr.appendChild(td);
?????????? table.appendChild(tr);
????????? }
?? ??? ??? ?
? ??? ?
???? // 創(chuàng)建刪除函數(shù)
???? 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 onclick = "deleteItem(this);return false;" >刪除</a></td>?? <!--在刪除按鈕上添加點(diǎn)擊事件? -->
?? ??? </tr>
?? ??? <tr>
?? ??? ?<td>xh002</td>
?? ??? ?<td>劉小芳</td>
?? ??? ?<td><a? onclick = "deleteItem(this);" >刪除</a></td>?? <!--在刪除按鈕上添加點(diǎn)擊事件? -->
?? ??? </tr> ?
?? ??? </table>
?? ??? <input type="button" value="添加一行"? onclick = "addTo();"/>?? <!--在添加按鈕上添加點(diǎn)擊事件? -->
?</body>
</html>