简体   繁体   English

基于.class的JQuery Accordion打开面板

[英]JQuery Accordion open panel based on .class

I have the following JQuery code to operate my accordion menu. 我有以下JQuery代码来操作我的手风琴菜单。

 <script type="text/javascript" src="_js/jquery-1.11.1.js"></script> <script type="text/javascript"> $(function() { var url = window.location.href; $('.menu a[href="'+url+'"]').addClass('current_page_item'); $("dd:not(:first)").hide(); $("dt a").click(function() { $("dd").slideUp("fast"); $(this).parent("dt").next("dd").slideDown("normal"); }); }); </script> 

How do I modify the code to detect the 'selected' class and open the corresponding panel from the following html script. 如何修改代码以检测“选定”类并从以下html脚本中打开相应的面板。

 <div id="SideMenu"> <dl> <li class="mtop">&nbsp;</li> <dt><a href="/index.php">Home</a></dt> <dt><a href="#">AA Theory</a></dt> <dd> <ul> <li class="litop"><a href="#">Link 5</a></li> <li class="limid"><a href="#">Link 6</a></li> <li class="limid"><a href="#">Link 7</a></li> <li class="libot"><a href="#">Link 8</a></li> </ul> </dd> <dt><a href="#">Glossaries</a></dt> <dd> <ul> <li class="litop"><a href="#">Astronomical</a></li> <li class="limid selected"><a href="#">Star Trek</a></li> <li class="limid"><a href="#">Science</a></li> <li class="libot"><a href="#">Mathematics</a></li> </ul> </dd> <li class="mbot">&nbsp;</li> </dl> </div> 

Using jQuery you can quickly access the selected element like this: 使用jQuery,您可以像这样快速访问所选元素:

$(".selected")

Knowing this, and knowing that this element is a <li> tag, we just need slideDown its parent <ul> once the page is loaded. 知道这一点并知道此元素是<li>标记后,只要加载页面,我们只需要slideDown其父元素<ul>即可。 Therefore you can simply add this code inside your $(document).ready(); 因此,您可以简单地将此代码添加到$(document).ready(); function: 功能:

$(".selected").parent().slideDown();

Here is the modified FIDDLE 这是修改后的FIDDLE

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

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