在這個(gè)編程作業(yè)里設(shè)置鼠標(biāo)移動(dòng)改變背景色可否用setAttribute
window.onload?=?function(){ ?????????????????? ?????//?鼠標(biāo)移動(dòng)改變背景,可以通過(guò)給每行綁定鼠標(biāo)移上事件和鼠標(biāo)移除事件來(lái)改變所在行背景色。 ??????function?color1(obj){ ??????????obj.style.backgroundColor?=?"#f2f2f2"; ??????}?? ??????function?color2(obj){ ??obj.style.backgroundColor?=?"#fff"; ??????} ??????var?lines?=?document.getElementsByTagName("tr"); ??????for(var?i=0;i<lines.length;i++){ ??????????lines[i].setAttribute("onmouseover","color1(this)"); ??????????lines[i].setAttribute("onmouseout","color2(this)");???????????????????? ??????} ? }
我這么寫的,但是運(yùn)行后沒有效果。想問問是否能這么使用?還是我哪里寫錯(cuò)了?
我發(fā)現(xiàn)很多同學(xué)代碼中沒有考慮對(duì)新添加的行增加這個(gè)改動(dòng)背景色的效果。請(qǐng)問如果不能用setAttribute來(lái)設(shè)置這個(gè)鼠標(biāo)移動(dòng)效果的話,有什么好方法可以讓新添加的行也有這個(gè)效果呢?
2018-06-04
newnode.setAttribute("onmouseover","style.backgroundColor='#f2f2f2'");
newnode.setAttribute("onmouseout","style.backgroundColor='#fff'");
這樣寫就可以了
2018-04-20
我就想請(qǐng)問你一下onmouseover與onmouseout是一個(gè)元素的屬性嗎?它們只是一個(gè)觸發(fā)事件。。setAttribute()這個(gè)函數(shù)能改變事件?? ?你先搞清楚再提問OK?
2018-04-20
setAttribute
是設(shè)置屬性的,直接設(shè)置顏色lines[i].setAttribute("style","background-color:red");