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

时间:2021-09-16 09:13:50

I have a simple accordion drop down menu that has four main levels. I can get the first one to open on page load and the last one to open on page load. What I would like is to get the third one to open on page load. Here is my HTML:

我有一个简单的手风琴下拉菜单,有四个主要级别。我可以让第一个在页面加载时打开,最后一个在页面加载时打开。我想要的是让第三个在页面加载时打开。这是我的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>

And here is my jquery code:

这是我的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");
    })
})

In the forth line of the code I can change the ".secondary:last" to ".secondary:first" and that will open the first accordion menu. But I would like to have the third accordion menu open on load. I have tried ".secondary:third" and ".secondary:2" but those do not work. Could someone help me with this?

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

1 个解决方案

#1


2  

you have to select the index of the element using ":eq()". the following will select the third matched element on a zero based index.

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

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

#1


2  

you have to select the index of the element using ":eq()". the following will select the third matched element on a zero based index.

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

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