課程
/前端開發(fā)
/JavaScript
/JavaScript進(jìn)階篇
是不是哪兒思路不對(duì)?
三個(gè)函數(shù)都沒效果
2017-05-01
源自:JavaScript進(jìn)階篇 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(){
? ? ? ? ? ? ? ? ??
? ? ?// 鼠標(biāo)移動(dòng)改變背景,可以通過給每行綁定鼠標(biāo)移上事件和鼠標(biāo)移除事件來改變所在行背景色。
? ? ? ? ?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";
? ? ? ? ?
? ? ? ? ?}
? ? ?
?
}
? ? ? // 編寫一個(gè)函數(shù),供添加按鈕調(diào)用,動(dòng)態(tài)在表格的最后一行添加子節(jié)點(diǎn);
? ? ?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>學(xué)號(hào)</th>
<th>姓名</th>
<th>操作</th>
? </tr> ?
<td>xh001</td>
<td>王小明</td>
<td><a href="javascript:removeChild(2);" >刪除</a></td> ? <!--在刪除按鈕上添加點(diǎn)擊事件 ?-->
? </tr>
<td>xh002</td>
<td>劉小芳</td>
<td><a href="javascript:removeChild(3);" >刪除</a></td> ? <!--在刪除按鈕上添加點(diǎn)擊事件 ?-->
? </table>
? <input type="button" value="添加一行" onclick="addTr()"/> ? <!--在添加按鈕上添加點(diǎn)擊事件 ?-->
?</body>
</html>
代碼呢?
magic_4 提問者
舉報(bào)
本課程從如何插入JS代碼開始,帶您進(jìn)入網(wǎng)頁動(dòng)態(tài)交互世界
1 回答哪位大神幫我來看下代碼?
4 回答請(qǐng)各位同學(xué)幫我看一下,附圖??!
1 回答幫我看下我的代碼
3 回答請(qǐng)大家?guī)臀铱聪挛业拇a哪里有問題
2 回答哪位前輩幫我看下,看看我寫的這段代碼有沒有問題
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號(hào)-11 京公網(wǎng)安備11010802030151號(hào)
購課補(bǔ)貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動(dòng)學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號(hào)
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(){
? ? ? ? ? ? ? ? ??
? ? ?// 鼠標(biāo)移動(dòng)改變背景,可以通過給每行綁定鼠標(biāo)移上事件和鼠標(biāo)移除事件來改變所在行背景色。
? ? ? ? ?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";
? ? ? ? ?
? ? ? ? ? ? ?}
? ? ? ? ?}
? ? ?
?
}
? ? ?
? ? ? // 編寫一個(gè)函數(shù),供添加按鈕調(diào)用,動(dòng)態(tài)在表格的最后一行添加子節(jié)點(diǎn);
? ? ?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>學(xué)號(hào)</th>
<th>姓名</th>
<th>操作</th>
? </tr> ?
? <tr>
<td>xh001</td>
<td>王小明</td>
<td><a href="javascript:removeChild(2);" >刪除</a></td> ? <!--在刪除按鈕上添加點(diǎn)擊事件 ?-->
? </tr>
? <tr>
<td>xh002</td>
<td>劉小芳</td>
<td><a href="javascript:removeChild(3);" >刪除</a></td> ? <!--在刪除按鈕上添加點(diǎn)擊事件 ?-->
? </tr> ?
? </table>
? <input type="button" value="添加一行" onclick="addTr()"/> ? <!--在添加按鈕上添加點(diǎn)擊事件 ?-->
?</body>
</html>
2017-05-02
代碼呢?