简体   繁体   English

jQuery Accordion - 在pageload上打开特定部分

[英]jQuery Accordion - open specific section on pageload

I have a rather basic implementation of a jQuery Accordion on a page (using 1.3.2, jQuery UI Core 1.72 and jQuery UI Accordion 1.7.2), and I wish to open the 2nd section when the page loads. 我在页面上有一个相当基本的jQuery Accordion实现(使用1.3.2,jQuery UI Core 1.72和jQuery UI Accordion 1.7.2),我希望在页面加载时打开第2部分。 i've tried numerous methods but nothing seems to work... 我尝试了很多方法,但似乎没有任何工作......

HEAD SCRIPT: 头文:

<script type="text/javascript"> $(function() {
    $("#accordion").accordion({
        event: "mouseover"
    });

});

BODY ACCORDION: 身体手风琴:

<div id="accordion">
<h3><a href="#">Headline 001</a></h3>
<div>
<ul>
     <li><a href="#1">Link 001</a></li>
     <li><a href="#2">Link 002</a></li>
     </ul>
</div>
<h3><a href="#">Headline 002</a></h3>
<div>
     <ul>
    <li><a href="#3">Link 003</a></li>
     <li><a href="#4">Link 004</a></li>
     </ul>
</div>

Any help would be greatly appreciated! 任何帮助将不胜感激!

$("#accordion").accordion({ active: 2, event: "mouseover" });

Should do the trick! 应该做的伎俩!

UPDATE UPDATE

if that doesn't work, try 如果这不起作用,试试吧

$("#accordion").accordion({  event: "mouseover" }).activate(2);

(NB this is updated to be a bit faster, thanks for the comments. To be honest, it should work with the 'active: 2' parameter, don't know why it didn't.) (注意这会更快一些,感谢评论。说实话,它应该与'active:2'参数一起使用,不知道它为什么没有。)

proper way to open a specific tab: 打开特定标签的正确方法:

$("#accordion").accordion({
    collapsible  : true,
    active       : false,
    heightStyle  : "content",
    navigation   : true
}); 

Set the option: 设置选项:

//$("#accordion").accordion('option', 'active' , "INSERT YOUR TAB INDEX HERE");
$("#accordion").accordion('option', 'active' , 1);

or you could work with a hash like this: 或者您可以使用这样的哈希:

if(location.hash){

    var tabIndex = parseInt(window.location.hash.substring(1));     
    $("#accordion").accordion('option', 'active' , tabIndex);

}

Vote if you use ;) 投票,如果你使用;)

Thanks 谢谢

Does the following work? 以下工作如何?

$(function() {
    $("#accordion").accordion({
        event: "mouseover",
        collapsible: true,
        active: 2
    });

});

尝试

$("#accordion").activate(index);

I have resolved this question a little different since I had to create a menu.php which we include I have included every single page. 我已经解决了这个问题有点不同,因为我必须创建一个menu.php,其中包括我已经包含的每一页。 In our project there was 1 accordion (a menu element with 2 submenus). 在我们的项目中有1个手风琴(菜单元素有2个子菜单​​)。 So when the visitor is on the submenus, the accordion is open and the selected link (which are highlighted using CSS, not jQuery) active. 因此,当访问者在子菜单上时,手风琴是打开的,并且所选链接(使用CSS而不是jQuery突出显示)处于活动状态。 But when the visitor is on a different page, the accordion works normally. 但是当访客在不同的页面上时,手风琴正常工作。

Here's the javascript: 这是javascript:

var containsParams = /teacher|student/i.test(window.location.href), //regexp with string params
accordion = $("li.accordion"); // the accordion as a global

accordion.accordion({ //accordion setup on every page
    animated : !containsParams,
    active : containsParams,
    collapsible : true,
    event : "click",
    header : "h2"
});

//I like to use "self calling methods" since many times I need a main onload event and this way it's clear for every page and my main function still remains
(function () {
    if (containsParams) accordion.accordion("activate", 0);
})();

Hope you like it. 希望你喜欢。 =] =]

Best regards! 最好的祝福! =] =]

You should write active: 1 instead of 2, because Accordion indexes sections from 0, not from one. 您应该写入active: 1而不是2,因为Accordion会将部分从0开始,而不是从1开始。 Working code will look like that: 工作代码将如下所示:

$("#accordion").accordion({ active: 1, event: "mouseover" });

Hope it will help a bit. 希望它会有所帮助。

As others have mentioned, the following will make it active on opening: 正如其他人所提到的,以下内容将使其在开放时更加活跃:

$("#accordion").accordion({ active: 1 });

It is active:1 since it is the 2nd of the accordion's index {0,1,2,...}; 它是active:1因为它是手风琴指数{0,1,2,...}; There seems to be some confusion in other answers as the element's contents contain the character " 2 "... 其他答案似乎有些混乱,因为元素的内容包含字符“ 2 ”......

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

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