變色為什么要嵌套才行了,直接寫沒效果呢
????? window.onload = function(){
????? var tr = document.getElementsByTagName("tr");
????????? for (var i=0;i<tr.length; i++) {
????????????? bgChange(tr[i]);
????????? }
???
? }
?// 鼠標移動改變背景,可以通過給每行綁定鼠標移上事件和鼠標移除事件來改變所在行背景色。
???? 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(),這樣寫就相當于當onmouseover時,執行下function()里的代碼,并不是給tr[i]的事件綁定方法
你試下直接在里面輸出i,發現無論你移動到哪一條,i的值都不會變
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
應該是可以的,你直接寫沒有效果,可能是你定錯了,可以把你說的沒有效果的代碼發出來看看