jquery手风琴菜单打开页面加载中间

时间:2015-02-27 15:24:43

标签: jquery accordion onload

我有一个简单的手风琴下拉菜单,有四个主要级别。我可以让第一个在页面加载时打开,最后一个在页面加载时打开。我想要的是让第三个在页面加载时打开。 这是我的HTML:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <link href="CSS/Accordion1.css" rel="stylesheet" />
    <script src="Scripts/JQuery.js" type="text/javascript"></script>
    <script src="Scripts/Accordion1.js" type="text/javascript"></script>

</head>
<body>
   <div id="Wrapper">
    <div class="accordion">
        <div class="primary">Employee Web Links:</div>
        <div class="secondary">
            SharePoint Webpart
            <!--Place Code Between these Markers-->

            <!--Place Code Between these Markers-->
        </div>
        <div class="primary">Phone Directory:</div>
        <div class="secondary">
            Sharepoint Webpart
            <!--Place Code Between these Markers-->

            <!--Place Code Between these Markers-->
        </div>
        <div class="primary">Web Search:</div>
        <div class="secondary">
            Sharepoint Webpart
            <!--Place Code Between these Markers-->

            <!--Place Code Between these Markers-->
        </div>
        <div class="primary">Industry News:</div>
        <div class="secondary">
            SharePoint Webpart
            <!--Place Code Between these Markers-->

            <!--Place Code Between these Markers-->
        </div>
    </div>
    </div> 
</body>
</html>

这是我的jquery代码:

$(function () {

    $(".accordion .secondary").hide();//hides containers
    $("div.accordion .secondary:last").slideDown('fast');//opens last container on load
    $(".accordion .primary").click(function () {
        $(this).next(".accordion .secondary").slideToggle("fast");
    })
})

在代码的第四行,我可以更改&#34; .secondary:last&#34; to&#34; .secondary:first&#34;这将打开第一个手风琴菜单。但是我希望第三个手风琴菜单能够正常打开。我试过&#34;。次要:第三次&#34;和&#34;。次要:2&#34;但那些不起作用。有人可以帮我这个吗?

1 个答案:

答案 0 :(得分:2)

你必须使用“:eq()”选择元素的索引。以下将选择基于零的索引上的第三个匹配元素。

$("div.accordion .secondary:eq(2)").slideDown('fast');