課程
/前端開發(fā)
/JavaScript
/JavaScript進階篇
是不是哪兒思路不對?
三個函數(shù)都沒效果
2017-05-01
源自:JavaScript進階篇 9-22
正在回答
顏色變換那里 ??this.style.backgroundColor="#f2f2f2";
絆笙瓜
<!DOCTYPE html>
<html>
?<head>
? <title> new document </title> ?
? <meta http-equiv="Content-Type" content="text/html; charset=gbk"/> ??
? <script type="text/javascript">?
? ? ? var table=document.getElementById("table");
? ? ? window.onload = function(){
? ? ? ? ? ? ? ? ??
? ? ?// 鼠標移動改變背景,可以通過給每行綁定鼠標移上事件和鼠標移除事件來改變所在行背景色。
? ? ? ? ?var trs=document.getElementsByName("tr");
? ? ? ? ?for(var i=0;i<trs.length;i++){
? ? ? ? ? ? ?trs[i].onmouseover()=function(){
? ? ? ? ? ? ? ? ?this.style.backgroundColor="#f2f2f2";
? ? ? ? ? ? ?}
? ? ? ? ? ? ?trs[i].onmouseout()=function(){
? ? ? ? ? ? ?trs[i].style.backgroundColor="transparent";
? ? ? ? ?
? ? ? ? ?}
? ? ?
?
}
? ? ? // 編寫一個函數(shù),供添加按鈕調(diào)用,動態(tài)在表格的最后一行添加子節(jié)點;
? ? ?function addTr(){
? ? ? ? ?var newTr=document.createElement("tr");
? ? ? ? ?var newTh1=document.createElement("td");
? ? ? ? ?var newTh2=document.createElement("td");
? ? ? ? ?var newTh3=document.createElement("td");
? ? ? ? ?newTh1.innerHTML="xh003";
? ? ? ? ?newTh2.innerHTML="李小六";
? ? ? ? ?newTh2.innerHTML="刪除";
? ? ? ? ?newTr.appendChild(newTh1);
? ? ? ? ?newTr.appendChild(newTh2);
? ? ? ? ?newTr.appendChild(newTh3);
? ? ? ? ?table.appendChild(newTr);
? ? ?}
? ?
? ? ?// 創(chuàng)建刪除函數(shù)
? ? ?function removeChild(x){
? ? ? ? ?for(var i=0;i<table.length;i++){
? ? ? ? ? ? ?if(i==x){
? ? ? ? ? ? ? ? ?table.removeChild(childNodes[i]);
? </script>?
?</head>?
?<body>?
? <table border="1" width="50%" id="table">
? <tr>
<th>學號</th>
<th>姓名</th>
<th>操作</th>
? </tr> ?
<td>xh001</td>
<td>王小明</td>
<td><a href="javascript:removeChild(2);" >刪除</a></td> ? <!--在刪除按鈕上添加點擊事件 ?-->
? </tr>
<td>xh002</td>
<td>劉小芳</td>
<td><a href="javascript:removeChild(3);" >刪除</a></td> ? <!--在刪除按鈕上添加點擊事件 ?-->
? </table>
? <input type="button" value="添加一行" onclick="addTr()"/> ? <!--在添加按鈕上添加點擊事件 ?-->
?</body>
</html>
代碼呢?
magic_4 提問者
舉報
本課程從如何插入JS代碼開始,帶您進入網(wǎng)頁動態(tài)交互世界
1 回答哪位大神幫我來看下代碼?
4 回答請各位同學幫我看一下,附圖?。?/p>
1 回答幫我看下我的代碼
2 回答哪位前輩幫我看下,看看我寫的這段代碼有沒有問題
3 回答請大家?guī)臀铱聪挛业拇a哪里有問題
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網(wǎng)安備11010802030151號
購課補貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動學習伙伴
掃描二維碼關注慕課網(wǎng)微信公眾號
2017-05-06
顏色變換那里 ??this.style.backgroundColor="#f2f2f2";
2017-05-02
<!DOCTYPE html>
<html>
?<head>
? <title> new document </title> ?
? <meta http-equiv="Content-Type" content="text/html; charset=gbk"/> ??
? <script type="text/javascript">?
? ? ? var table=document.getElementById("table");
? ? ? window.onload = function(){
? ? ? ? ? ? ? ? ??
? ? ?// 鼠標移動改變背景,可以通過給每行綁定鼠標移上事件和鼠標移除事件來改變所在行背景色。
? ? ? ? ?var trs=document.getElementsByName("tr");
? ? ? ? ?for(var i=0;i<trs.length;i++){
? ? ? ? ? ? ?trs[i].onmouseover()=function(){
? ? ? ? ? ? ? ? ?this.style.backgroundColor="#f2f2f2";
? ? ? ? ? ? ?}
? ? ? ? ? ? ?trs[i].onmouseout()=function(){
? ? ? ? ? ? ?trs[i].style.backgroundColor="transparent";
? ? ? ? ?
? ? ? ? ? ? ?}
? ? ? ? ?}
? ? ?
?
}
? ? ?
? ? ? // 編寫一個函數(shù),供添加按鈕調(diào)用,動態(tài)在表格的最后一行添加子節(jié)點;
? ? ?function addTr(){
? ? ? ? ?var newTr=document.createElement("tr");
? ? ? ? ?var newTh1=document.createElement("td");
? ? ? ? ?var newTh2=document.createElement("td");
? ? ? ? ?var newTh3=document.createElement("td");
? ? ? ? ?newTh1.innerHTML="xh003";
? ? ? ? ?newTh2.innerHTML="李小六";
? ? ? ? ?newTh2.innerHTML="刪除";
? ? ? ? ?newTr.appendChild(newTh1);
? ? ? ? ?newTr.appendChild(newTh2);
? ? ? ? ?newTr.appendChild(newTh3);
? ? ? ? ?table.appendChild(newTr);
? ? ?}
? ?
? ? ?
? ? ?// 創(chuàng)建刪除函數(shù)
? ? ?function removeChild(x){
? ? ? ? ?for(var i=0;i<table.length;i++){
? ? ? ? ? ? ?if(i==x){
? ? ? ? ? ? ? ? ?table.removeChild(childNodes[i]);
? ? ? ? ? ? ?}
? ? ? ? ?}
? ? ?}
? </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:removeChild(2);" >刪除</a></td> ? <!--在刪除按鈕上添加點擊事件 ?-->
? </tr>
? <tr>
<td>xh002</td>
<td>劉小芳</td>
<td><a href="javascript:removeChild(3);" >刪除</a></td> ? <!--在刪除按鈕上添加點擊事件 ?-->
? </tr> ?
? </table>
? <input type="button" value="添加一行" onclick="addTr()"/> ? <!--在添加按鈕上添加點擊事件 ?-->
?</body>
</html>
2017-05-02
代碼呢?