變色為什么要嵌套才行了,直接寫沒效果呢
????? window.onload = function(){
????? var tr = document.getElementsByTagName("tr");
????????? for (var i=0;i<tr.length; i++) {
????????????? bgChange(tr[i]);
????????? }
???
? }
?// 鼠標(biāo)移動(dòng)改變背景,可以通過給每行綁定鼠標(biāo)移上事件和鼠標(biāo)移除事件來改變所在行背景色。
???? function bgChange(a) {
???????? a.onmouseover=function()
???????? {
???????? a.style.backgroundColor="#f2f2f2";
???????? }
??????? a.onmouseout=function()
???????? {
???????? a.style.backgroundColor="#fff";
???????? }
?}
????
2015-02-11
你這樣寫就可以了
var tr = document.getElementsByTagName("tr");
? ? ? ? ? for (var i=0;i<tr.length; i++) {
? ?
? ? ? ? ? ?tr[i].onmouseover=function()
? ? ? ? ? ? ?{
? ? ? ? ? ? ? ? this.style.backgroundColor="#f2f2f2";
? ? ? ? ? ? ?}
? ? ? ? ?tr[i].onmouseout=function()
? ? ? ? ? ? ?{
? ? ? ? ? ? ? ? this.style.backgroundColor="#fff";
? ? ? ? ? ? ?}
? }
2015-02-11
問題出在onmouseover=function(),這樣寫就相當(dāng)于當(dāng)onmouseover時(shí),執(zhí)行下function()里的代碼,并不是給tr[i]的事件綁定方法
你試下直接在里面輸出i,發(fā)現(xiàn)無論你移動(dòng)到哪一條,i的值都不會(huì)變
2015-02-10
window.onload = function(){
????? var tr = document.getElementsByTagName("tr");
????????? for (var i=0;i<tr.length; i++) {
?????????? tr[i].onmouseover=function()
???????? {
???????? a.style.backgroundColor="#f2f2f2";
???????? }
???????? tr[i].onmouseout=function()
???????? {
???????? a.style.backgroundColor="#fff";
???????? }
??? }
?
? }
?
2015-02-10
應(yīng)該是可以的,你直接寫沒有效果,可能是你定錯(cuò)了,可以把你說的沒有效果的代碼發(fā)出來看看