2010-09-23 51 views
1

在這個javascript hide/show示例中,如何在選中div時關閉所有其他div?javascript hide/show example - close divs

<script TYPE="text/JavaScript"> 
    function show_hide(id, show) 
    { 
     if (el = document.getElementById(id)) 
     { 
     if (null==show) show = el.style.display=='none'; 
     el.style.display = (show ? '' : 'none'); 
     } 
    } 
    </script> 

&不要告訴我使用jQuery,原因是其不會在我們使用一些移動的環境中運行。

回答

1

描述我會做這種

var alldivs = document.getElementsByTagName("DIV"); 
for (var i=0;i<alldivs.length;i++){ 
var odiv = alldivs[i]; 
//we only need "other" divs, not the one we're working on 
if ((odiv.id) && (odiv.id!=id)) { 
    odiv.style.display="none"; 
} 
} 

(+感謝像不要求一個jQuery的解決方案:)

+0

作品完美! – robert 2010-09-23 21:00:38