我有一個包含一些子菜單項的垂直菜單,此子菜單在點擊時打開。 但問題是,當我點擊子菜單項之一,菜單關閉,而我希望它保持開放,因爲當我瀏覽的子菜單項 這裏的HTML:點擊子菜單必須保持打開狀態
<div class="ul_container">
<ul class="mainnav" id="nav" style="list-style:none;">
<li><a id="active" href="index.html"><strong>HOME</strong></a></li>
<li><a href="javascript:void(0)" onclick="toggleID('sub1')">COLLECTIONS</a>
<ul class="subnav" id="sub1" style="display:none">
<li class="first"><a href="Collections_PE_13.html">spring/summer 2013</a>
<li class="first"><a href="Collections_AI_13.html">autumn/winter 2013</a>
<li class="first"><a href="Collections_AI_12.html">autumn/winter 2012</a>
<li class="first"><a href="Collections.html">autumn/winter 2011</a>
</ul>
</li>
<li><a href="Portrait.html">PORTRAIT</a></li>
<li><a href="Heritage.html">HERITAGE</a></li>
<li><a href="Press.html">PRESS</a></li>
<li><a href="Contacts.html">CONTACTS</a></li>
</ul>
</div>
和JS
function toggleID(IDS) {
var sel = document.getElementById('nav').getElementsByTagName('ul');
for (var i=0; i<sel.length; i++) {
if (sel[i].id != IDS) { sel[i].style.display = 'none'; }
}
sel = document.getElementById(IDS);
sel.style.display = (sel.style.display != 'block') ? 'block' : 'none';
}
您的html標記有一些錯誤。那些subnav li沒有關閉 –