課程
/前端開發(fā)
/JavaScript
/JavaScript進階篇
為什么新增一行后,鼠標(biāo)滑過改變背景色不好使了?
2016-08-01
源自:JavaScript進階篇 9-22
正在回答
因為你沒給添加的行設(shè)置鼠標(biāo)劃過的事件。下面這幾句就是給節(jié)點添加鼠標(biāo)劃過事件的函數(shù)(只有初始化時使用到,所以只有一開始三行有反應(yīng))
? ? ? ? ? var trs = document.getElementsByTagName('tr');
? ? ? ? ? ? for(var i=1,l=trs.length;i<l;i++){
? ? ? ? ? ? trs[i].onmouseover=function(){
? ? ? ? ? ? ? ? this.style.backgroundColor="#ccc";
? ? ? ? ? ? }
? ? ? ? ? ? trs[i].onmouseout=function(){
? ? ? ? ? ? ? ? this.style.backgroundColor="#fff";
修改方法1:
把以上函數(shù)寫成一個函數(shù),這里命名為change(),然后這樣:
? ? ? window.onload = function(){
? ? ? ? ? change(); ? ??}
function change(){?
}
最后在addto函數(shù)最后調(diào)用change函數(shù)
方法2:
直接復(fù)制上面代碼到addto函數(shù)最后面
Aimeesui 提問者
舉報
本課程從如何插入JS代碼開始,帶您進入網(wǎng)頁動態(tài)交互世界
2 回答添加行為什么鼠標(biāo)滑過沒有背景顏色
3 回答鼠標(biāo)經(jīng)過改變背景顏色問題
1 回答鼠標(biāo)移到不同行時背景色改變?
1 回答為什么行不隨著鼠標(biāo)的移動而改變背景顏色
1 回答求解,為什么鼠標(biāo)移過背景沒改變啊???
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網(wǎng)安備11010802030151號
購課補貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號
2016-08-01
因為你沒給添加的行設(shè)置鼠標(biāo)劃過的事件。下面這幾句就是給節(jié)點添加鼠標(biāo)劃過事件的函數(shù)(只有初始化時使用到,所以只有一開始三行有反應(yīng))
? ? ? ? ? var trs = document.getElementsByTagName('tr');
? ? ? ? ? ? for(var i=1,l=trs.length;i<l;i++){
? ? ? ? ? ? trs[i].onmouseover=function(){
? ? ? ? ? ? ? ? this.style.backgroundColor="#ccc";
? ? ? ? ? ? }
? ? ? ? ? ? trs[i].onmouseout=function(){
? ? ? ? ? ? ? ? this.style.backgroundColor="#fff";
? ? ? ? ? ? }
修改方法1:
把以上函數(shù)寫成一個函數(shù),這里命名為change(),然后這樣:
? ? ? window.onload = function(){
? ? ? ? ? change(); ? ??}
function change(){?
? ? ? ? ? var trs = document.getElementsByTagName('tr');
? ? ? ? ? ? for(var i=1,l=trs.length;i<l;i++){
? ? ? ? ? ? trs[i].onmouseover=function(){
? ? ? ? ? ? ? ? this.style.backgroundColor="#ccc";
? ? ? ? ? ? }
? ? ? ? ? ? trs[i].onmouseout=function(){
? ? ? ? ? ? ? ? this.style.backgroundColor="#fff";
? ? ? ? ? ? }
}
最后在addto函數(shù)最后調(diào)用change函數(shù)
方法2:
直接復(fù)制上面代碼到addto函數(shù)最后面