简体   繁体   English

简单的JQuery手风琴菜单:默认打开第二个

[英]Simple JQuery Accordion menu: open second by default

I am using a simple jquery accordian menu. 我正在使用一个简单的jQuery Accordian菜单。 But I want the second item in the list to be open by default rather than the :first 但是我希望列表中的第二项默认为打开,而不是:first

How can this be done? 如何才能做到这一点?

function initMenu()
{
     $('#menu ul').hide();
     $('#menu ul:first').show();
     $('#menu li a').click(function()
     {
          var checkElement = $(this).next();
          if((checkElement.is('ul')) && (checkElement.is(':visible')))
          {
               return false;
          }
          if((checkElement.is('ul')) && (!checkElement.is(':visible')))
          {
               $('#menu ul:visible').slideUp('normal');
               checkElement.slideDown('normal');
               return false;
          }
     });
}
$(document).ready(function() {initMenu();});

I am also wondering if I could use a selected class to help make this more dynamic: 我也想知道是否可以使用选定的类来使其变得更加动态:

 <div id="work_menu"> <ul id="side_menu" class="nav_categories"> <li> <a href="celebratory-use">Celebratory Use</a> <ul> <li><a href="platters">Platters</a></li> <li><a href="celebratory-servers">Servers</a></li> </ul> </li> <li><a class="selected" href="daily-use">Daily Use</a> <ul> <li><a href="bowls">Bowls</a></li> <li><a href="butter-dishes">Butter Dishes</a></li> <li><a href="mugs">Mugs</a></li> </ul> </li> </ul> </div> 

Try $('#menu ul:eq(1)').show(); 尝试$('#menu ul:eq(1)').show(); instead of $('#menu ul:first').show(); 而不是$('#menu ul:first').show(); , this would apply .show() to the second element ul element in #menu . 这将适用.show()到第二元件ul在元件#menu Without the HTML it is difficult to see if this is the proper solution. 如果没有HTML,将很难看出这是否是正确的解决方案。

To make it so that the ul coming after the item given the .selected class is shown, change $('#menu ul:eq(1)').show(); 为了使其显示给定.selected类的项之后的ul ,请更改$('#menu ul:eq(1)').show(); .selected to $('#side_menu li > a.selected + ul').show(); $('#side_menu li > a.selected + ul').show(); , so long as the .selected class is always applied to an <a> . ,只要.selected类始终应用于<a> If you wish to apply it to another sibling element of the <li> , then change the selector to $('#side_menu li .selected + ul').show(); 如果希望将其应用于<li>另一个同级元素,则将选择器更改为$('#side_menu li .selected + ul').show(); , which is a little more generalizable. ,这更具通用性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM