為啥1行2不行呢
1:
? ? ?/* window.onload = function(){
? ? ? ? var hang=document.getElementsByTagName('tr');
? ? ? ? for(var i=0;i<hang.length;i++){
? ? ? ? ? ?bdc(hang[i]);
? ? ? ? ? ? }
? ? ? ? }
? ? ?function bdc(can){
? ? ? ? ?can.onmouseover=function(){
? ? ? ? ? ? ?can.style.backgroundColor="#f2f2f2";
? ? ? ? ?}
? ? ? ? ?can.onmouseout=function(){
? ? ? ? ? ? ?can.style.backgroundColor="#fff";
? ? ? ? ?}
? ? ?}*/
2: ? ??
? ? window.onload = function(){
? ? ? ? var hang=document.getElementsByTagName('tr');
? ? ? ? for(var i=0;i<hang.length;i++){
? ? ? ? ? ? hang[i].onmouseover=function(){
? ? ? ? ? ? ? ? hang[i].style.backgroundColor="#f2f2f2";
? ? ? ? ? ? }
? ? ? ? ? ? hang[i].onmouseout=function(){
? ? ? ? ? ? ? ? hang[i].style.backgroundColor="#fff";
? ? ? ? ? ? }
? ? ? ? }
? ? }
2016-02-25
2改成
?for(var i=0;i<hang.length;i++){
? ? ? ? ? ? hang[i].onmouseover=function(){
? ? ? ? ? ? ? ? this.style.backgroundColor="#f2f2f2";
? ? ? ? ? ? }
? ? ? ? ? ? hang[i].onmouseout=function(){
? ? ? ? ? ? ? ? this.style.backgroundColor="#fff";
? ? ? ? ? ? }
}
就好了呢~
因為在你嵌套的函數(shù)里面并不知道hang[i]是誰啊~
作用域的問題
2016-02-25
如樓上所言改成this試試。1的函數(shù)里帶參數(shù)