简体   繁体   English

如何打开特定的手风琴div?

[英]How to open a specific accordion div?

This seems a straight forward question but I can't seem to find the answer to this. 这似乎是一个直截了当的问题,但我似乎找不到答案。 I'm trying to expand, open, collapse (whichever youd like to call it) a specific accordion. 我正在尝试扩展,打开,折叠(任何您想称呼它的地方)特定的手风琴。 Say I have three panels, with ids #panel1, #panel2, and #panel3. 假设我有三个面板,分别是ID#panel1,#panel2和#panel3。 And I have the panel headings with #p1, #p2, #p3. 我的面板标题是#p1,#p2,#p3。

If I click on the headings everything works great, #p1 will open #panel1, like how accordion is supposed to work. 如果单击标题,一切正常,#p1将打开#panel1,就像手风琴应该如何工作一样。 But I have a seperate link on the page which I'd like it to open #panel2. 但是我在页面上有一个单独的链接,希望它打开#panel2。

slideToggle, and slideDown cause issues since if you click the heading after, it messes with accordion. slideToggle和slideDown会引起问题,因为如果您单击后面的标题,则会与手风琴混淆。 I don't want to use hash so I added this to onclick on the link I want to open panel2. 我不想使用哈希,所以我将其添加到要打开panel2的链接的onclick上。

$("#p2").click();

This works great, except if you click on that link again, it'll close the accordion div, I'd like it to stay open. 这很好用,除非再次单击该链接,它将关闭手风琴div,我希望它保持打开状态。 Any ideas? 有任何想法吗? Thanks much in advance. 在此先感谢。

You can make the second panel active by setting the active option . 您可以通过设置激活选项来激活第二个面板。

$("#accordion").accordion("option", "active", 1);

Or assuming your accordion HTML is default jQuery UI syntax (header/div/header/div/etc.), you can get the index of the panel pragmatically with $("#panel2").index("#accordion div") 或者假定您的手风琴HTML是默认的jQuery UI语法(header / div / header / div / $("#panel2").index("#accordion div") ),则可以使用$("#panel2").index("#accordion div")实用地获取面板的索引$("#panel2").index("#accordion div")

$("#accordion").accordion("option", "active", $("#panel2").index("#accordion div"));

use an 'open' class: If it has the open class, close it and remove 'open' class. 使用“打开”类:如果具有打开类,则将其关闭并删除“打开”类。 otherwise, open it, and add an 'open' class. 否则,请打开它,并添加一个“打开”类。 instead of clicking it with the external link, just slide it up and add the open class. 无需单击外部链接,只需向上滑动并添加打开的类即可。 Here is a very basic jquery accordian. 这是一个非常基本的jQuery Accordian。 http://jsfiddle.net/L8fMq/2/ http://jsfiddle.net/L8fMq/2/

<style>
.contents {display:none; border: 1px solid #ccc; }
.header {padding: 10px; border: 1px solid #ccc; background-color: #ddd;
</style>

<a class='externalLink'>Open Panel 2</a>
<div class='panel panel1'>
    <div class='header'>Panel 1</div>
    <div class='contents'>
        <p>A bunch of content</p>
    </div>
</div>
<div class='panel panel2'>
    <div class='header'>Panel 2</div>
    <div class='contents'>
        <p>A bunch of content</p>
    </div>
</div>
    <div class='panel panel3'>
    <div class='header'>Panel 3</div>
    <div class='contents'>
        <p>A bunch of content</p>
    </div>
</div>

$('.panel .header').click(function(){
    if($(this).parent().hasClass("open")){
        $(this).parent().find(".contents").slideUp();
        $(this).parent().removeClass("open");
    }
    else {
        $(this).parent().find(".contents").slideDown();
        $(this).parent().addClass("open");
    }
        $(this).parent().siblings().find(".contents").slideUp();
    });
$('.externalLink').click(function(){
    $('.panel2 .contents').slideDown().parent().addClass("open");

    $(this).parent().siblings().find(".contents").slideUp();
});

This should do the trick: 这应该可以解决问题:

$("#p2").click(function () {
    $("#accordion").accordion("option", "active", 1);
});

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

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