為什么這么寫提交上去表格直接就是紅色?
? ? window.onload = function(){
? ? ? ? trlist=document.getElementsByTagName("tr");
? ? ? ? for(var i=0;i<trlist.length;i++){
? ? ? ? ?trlist[i].onmouseover=bgcChange(trlist[i]);
? ? ? ? ?trlist[i].onmouseout=function(){this.style.backgroundColor="#fff"}
? ? ? ? }
}
? ? function bgcChange(a){
? ? ? ? a.style.backgroundColor="red";
? ? }
2016-10-30
如果直接這么寫:
這里bgcChange是一個自執行函數,而并不是事件函數,因此一加載頁面就會執行;
但也不能這么寫:
這么也i值為定數(i為trlist.length),因此要寫成你的mouseout事件的格式,用this.
2016-10-30
方便把全部代碼貼出來嗎
2016-10-29
this就是代表trlist[i],然后this.style.backgroundColor=“red”就是直接給他們每一個都設置了顏色!
function bgcChange(a){
? ? ? ? a.style.backgroundColor="red";
? ? }
這個函數和
?trlist[i].onmouseout=function(){this.style.backgroundColor="#fff"}只要一個就好了
你用了兩種方法設置了顏色
2016-10-29
因為window.onload意思就是頁面一加載就運行!bgcChange(trlist[i]);調用了函數? function bgcChange(a)所以為紅色