2011-03-23 84 views
0

Javascript循环继续,或中断显示?Javascript循环继续,或打破显示?

这是我的代码使用了节目或table

if (type=='SHOW') { 
    for(var i = 0; i<list_tr.length; i++) { 
      document.getElementById(list_tr[i]).style.display = ''; 
    } 
else if (type=='SHOW_EXCEPT'){ 
    document.getElementById(list_tr[0]).style.display = 'none'; 
    document.getElementById(list_tr[1]).style.display = ''; 
    for(var i = 2; i<list_tr.length; i++) { 
     document.getElementById(list_tr[i]).style.display = 'none'; 
    } 
}else{ 
    for(var i = 0; i<list_tr.length; i++) { 
     document.getElementById(list_tr[i]).style.display = 'none'; 
    } 
} 

隐藏一些数据在else情况:

我想隐藏的所有,但显示list_tr[2]如果type=='SHOW_EXCEPT'

我该怎么办在这种情况下?

感谢

回答

1
if (type=='SHOW') 
{ 
    for(var i = 0; i<list_tr.length; i++) 
    {    
     document.getElementById(list_tr[i]).style.display = ''; 
    } 
} 
else 
{  
    for(var i = 0; i<list_tr.length; i++) 
    { 
    if (i==2||type=='SHOW_EXCEPT') 
     document.getElementById(list_tr[i]).style.display = ''; 
    else 
     document.getElementById(list_tr[i]).style.display = 'none'; 
    } 
} 

这应该做的伎俩,如果我已经明白这个问题正确

+0

使用'.style.display ='block''而不是'.style.display ='''。 – 2011-03-23 10:41:09

+0

是的,我更新了问题,但是如果有使用break的好方法,继续, – kn3l 2011-03-23 11:01:27

1

不知道我理解正确的问题,但它看起来像你需要else if

if (type == 'SHOW') { 
    // Do something 
} else if (type == 'SHOW_EXCEPT') { 
    // Do something else 
} else { 
    // Do something different 
} 
+0

是的,我试过了,请看看我的更新问题,有好的方法休息,继续.. – kn3l 2011-03-23 10:59:28