jQuery 手风琴展开所有 div

发布于 2024-08-27 11:56:19 字数 34 浏览 7 评论 0原文

当页面加载或事件发生时是否可以展开所有组件? 谢谢!!

Is it possible to expand all components when page is load or when an event occurs?
Thanks!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

远山浅 2024-09-03 11:56:19

只需使用这个

$('#accordion .ui-accordion-content').show();

Simply use this

$('#accordion .ui-accordion-content').show();
灼疼热情 2024-09-03 11:56:19

不,如果您指的是标签所示的手风琴。来自 jQuery。

注意:如果您想要多个部分
立即打开,不要使用手风琴

http://docs.jquery.com/ UI/API/1.8/手风琴

No, if you are referring to accordion as your tag states. From jQuery.

NOTE: If you want multiple sections
open at once, don't use an accordion

http://docs.jquery.com/UI/API/1.8/Accordion

彩扇题诗 2024-09-03 11:56:19

我推荐这个插件多开手风琴

// this will make the second tab by default opened (index starts from 0)
$('#multiAccordion').multiAccordion({active: 1 }); 

// [ OR ]
// supports multiple tabs to be opened by default
$('#multiAccordion').multiAccordion({active: [1, 2, 3] }); 

// show all tabs
$('#multiAccordion').multiAccordion({active: 'all' });

// hide all tabs
$('#multiAccordion').multiAccordion({active: 'none' });

// you can set the options as any jQuery UI plugin using option method
$('#multiAccordion').multiAccordion('option', 'active', 'all');

I would recommend this plugin Multi-open Accordion

// this will make the second tab by default opened (index starts from 0)
$('#multiAccordion').multiAccordion({active: 1 }); 

// [ OR ]
// supports multiple tabs to be opened by default
$('#multiAccordion').multiAccordion({active: [1, 2, 3] }); 

// show all tabs
$('#multiAccordion').multiAccordion({active: 'all' });

// hide all tabs
$('#multiAccordion').multiAccordion({active: 'none' });

// you can set the options as any jQuery UI plugin using option method
$('#multiAccordion').multiAccordion('option', 'active', 'all');
好多鱼好多余 2024-09-03 11:56:19

我喜欢这个切换框的实现:

http://jsfiddle.net/kevinPHPkevin/mZhTY/107/

<div class="toggle-box">
  <div class="toggle-title">Toggle 1</div>
  <div class="toggle-content">
    <p>text1.</p>
  </div>
  <div class="toggle-title">Toggle 2</div>
  <div class="toggle-content">
    <p>text2.</p>
  </div>
  <div class="toggle-title">Toggle 3</div>
  <div class="toggle-content">
    <p>text3</p>
  </div>
</div>

发现这个在

我无法让 jQuery UI 手风琴选项卡在使用时更改颜色(如 a:visited),可以吗?

I like this implementation of a toggle box:

http://jsfiddle.net/kevinPHPkevin/mZhTY/107/

<div class="toggle-box">
  <div class="toggle-title">Toggle 1</div>
  <div class="toggle-content">
    <p>text1.</p>
  </div>
  <div class="toggle-title">Toggle 2</div>
  <div class="toggle-content">
    <p>text2.</p>
  </div>
  <div class="toggle-title">Toggle 3</div>
  <div class="toggle-content">
    <p>text3</p>
  </div>
</div>

Found this at

I can't make a jQuery UI accordion tab change colors when used (like a:visited), can I?

不念旧人 2024-09-03 11:56:19

哈哈,这里有很多答案,但最简单的解决方案似乎不是。答案是“是”和“不是”。你不能使用“传统”命令,但是你可以使用“类”并自己编写JS,即 超级简单

只需使用 jQueryUI 的类和一点 JS 制作您自己的手风琴,就可以开始了。它非常简单,并且可以使用 jQueryUI ThemeRoller 进行维护。如果您想要“官方手风琴小部件”提供的“x-tra”选项,您可以使用更多的 CSS 或 JS 来管理所有这些选项。真的没那么难。下面是我将发布的代码的工作示例的链接。我所做的只是将他们的类逐字复制到我的 HTML 布局中。 HTML 的布局仍然像任何其他 jQueryUI 手风琴一样,只不过我们手动应用类而不是让 jQuery 来完成。看看下面就明白我的意思了。

或者当然你可以获取这个 xtra、jquery 主题就绪插件< /a>

jsFiddle


HTML

<div id="StickyNotes">
    <div class="container">
        <div id="accordion" class="ui-accordion ui-widget ui-helper-reset">
            <h3 class="ui-accordion-header ui-helper-reset ui-state-default ui-accordion-icons ui-corner-top ui-accordion-header-active ui-state-active">
                <span class="ui-accordion-header-icon ui-icon ui-icon-triangle-1-s"></span>
                Section 1
            </h3>
            <div class="ui-accordion-content ui-helper-reset ui-widget-content ui-corner-bottom ui-accordion-content-active">
                <p>
                    Mauris mauris ante, blandit et, ultrices a, suscipit eget, quam. Integer
                    ut neque. Vivamus nisi metus, molestie vel, gravida in, condimentum sit
                    amet, nunc. Nam a nibh. Donec suscipit eros. Nam mi. Proin viverra leo ut
                    odio. Curabitur malesuada. Vestibulum a velit eu ante scelerisque vulputate.
                </p>
            </div>
            <h3 class="ui-accordion-header ui-helper-reset ui-state-default ui-accordion-icons ui-corner-top ui-accordion-header-active ui-state-active">
                <span class="ui-accordion-header-icon ui-icon ui-icon-triangle-1-s"></span>
                Section 2
            </h3>
            <div class="ui-accordion-content ui-helper-reset ui-widget-content ui-corner-bottom ui-accordion-content-active">
                <p>
                    Sed non urna. Donec et ante. Phasellus eu ligula. Vestibulum sit amet
                    purus. Vivamus hendrerit, dolor at aliquet laoreet, mauris turpis porttitor
                    velit, faucibus interdum tellus libero ac justo. Vivamus non quam. In
                    suscipit faucibus urna.
                </p>
            </div>
            <h3 class="ui-accordion-header ui-helper-reset ui-state-default ui-accordion-icons ui-corner-top ui-accordion-header-active ui-state-active">
                <span class="ui-accordion-header-icon ui-icon ui-icon-triangle-1-s"></span>
                Section 3
            </h3>
            <div class="ui-accordion-content ui-helper-reset ui-widget-content ui-corner-bottom ui-accordion-content-active">
                <p>
                    Nam enim risus, molestie et, porta ac, aliquam ac, risus. Quisque lobortis.
                    Phasellus pellentesque purus in massa. Aenean in pede. Phasellus ac libero
                    ac tellus pellentesque semper. Sed ac felis. Sed commodo, magna quis
                    lacinia ornare, quam ante aliquam nisi, eu iaculis leo purus venenatis dui.
                </p>
                <ul>
                    <li>List item one</li>
                    <li>List item two</li>
                    <li>List item three</li>
                </ul>
            </div>
            <h3 class="ui-accordion-header ui-helper-reset ui-state-default ui-accordion-icons ui-corner-top ui-accordion-header-active ui-state-active">
                <span class="ui-accordion-header-icon ui-icon ui-icon-triangle-1-s"></span>
                Section 4
            </h3>
            <div class="ui-accordion-content ui-helper-reset ui-widget-content ui-corner-bottom ui-accordion-content-active">
                <p>
                    Cras dictum. Pellentesque habitant morbi tristique senectus et netus
                    et malesuada fames ac turpis egestas. Vestibulum ante ipsum primis in
                    faucibus orci luctus et ultrices posuere cubilia Curae; Aenean lacinia
                    mauris vel est.
                </p>
                <p>
                    Suspendisse eu nisl. Nullam ut libero. Integer dignissim consequat lectus.
                    Class aptent taciti sociosqu ad litora torquent per conubia nostra, per
                    inceptos himenaeos.
                </p>
            </div>
        </div>
    </div>
</div>

JavaScript

$(function() {
    $("#accordion > h3").on("click", function(e) {
        $(this).next().slideToggle(function(e) {
            if ($(this).is(":visible")) {
                $(this).addClass("ui-accordion-content-active")
                .prev().toggleClass("ui-corner-all ui-corner-top").addClass("ui-accordion-header-active ui-state-active")
                .children(".ui-accordion-header-icon").toggleClass("ui-icon-triangle-1-e ui-icon-triangle-1-s");
            }
            else {
                $(this).removeClass("ui-accordion-content-active")
                .prev().toggleClass("ui-corner-all ui-corner-top").removeClass("ui-accordion-header-active ui-state-active")
                .children(".ui-accordion-header-icon").toggleClass("ui-icon-triangle-1-e ui-icon-triangle-1-s");
            }
        });
    })
    .hover(function(e) { $(this).toggleClass("ui-state-hover"); });
})

Lol, many answers on here, but the easiest solution doesn't seem to be. The answer is "yes" and "no". You can't use the "traditional" command, however you can use the "classes" and write the JS yourself, which is SUPER SIMPLE!

Just make your own Accordion using jQueryUI's classes and a little bit of JS and you're good to go. It's VERY easy and maintains with your jQueryUI ThemeRoller. If you want the "x-tra" options the "official accorrdion widget" provides, you can manage all of them with either a little more CSS or JS. Really Not that hard. Below is a link to the working example of the code I will post. All I simply did was copy their classes to my HTML layout verbatim. The HTML is still laid out just like any other jQueryUI accordion, except we apply the classes manually instead of letting jQuery do it. Look below to see what I mean.

Or of course you can get this xtra, jquery-theme ready plugin

jsFiddle


The HTML

<div id="StickyNotes">
    <div class="container">
        <div id="accordion" class="ui-accordion ui-widget ui-helper-reset">
            <h3 class="ui-accordion-header ui-helper-reset ui-state-default ui-accordion-icons ui-corner-top ui-accordion-header-active ui-state-active">
                <span class="ui-accordion-header-icon ui-icon ui-icon-triangle-1-s"></span>
                Section 1
            </h3>
            <div class="ui-accordion-content ui-helper-reset ui-widget-content ui-corner-bottom ui-accordion-content-active">
                <p>
                    Mauris mauris ante, blandit et, ultrices a, suscipit eget, quam. Integer
                    ut neque. Vivamus nisi metus, molestie vel, gravida in, condimentum sit
                    amet, nunc. Nam a nibh. Donec suscipit eros. Nam mi. Proin viverra leo ut
                    odio. Curabitur malesuada. Vestibulum a velit eu ante scelerisque vulputate.
                </p>
            </div>
            <h3 class="ui-accordion-header ui-helper-reset ui-state-default ui-accordion-icons ui-corner-top ui-accordion-header-active ui-state-active">
                <span class="ui-accordion-header-icon ui-icon ui-icon-triangle-1-s"></span>
                Section 2
            </h3>
            <div class="ui-accordion-content ui-helper-reset ui-widget-content ui-corner-bottom ui-accordion-content-active">
                <p>
                    Sed non urna. Donec et ante. Phasellus eu ligula. Vestibulum sit amet
                    purus. Vivamus hendrerit, dolor at aliquet laoreet, mauris turpis porttitor
                    velit, faucibus interdum tellus libero ac justo. Vivamus non quam. In
                    suscipit faucibus urna.
                </p>
            </div>
            <h3 class="ui-accordion-header ui-helper-reset ui-state-default ui-accordion-icons ui-corner-top ui-accordion-header-active ui-state-active">
                <span class="ui-accordion-header-icon ui-icon ui-icon-triangle-1-s"></span>
                Section 3
            </h3>
            <div class="ui-accordion-content ui-helper-reset ui-widget-content ui-corner-bottom ui-accordion-content-active">
                <p>
                    Nam enim risus, molestie et, porta ac, aliquam ac, risus. Quisque lobortis.
                    Phasellus pellentesque purus in massa. Aenean in pede. Phasellus ac libero
                    ac tellus pellentesque semper. Sed ac felis. Sed commodo, magna quis
                    lacinia ornare, quam ante aliquam nisi, eu iaculis leo purus venenatis dui.
                </p>
                <ul>
                    <li>List item one</li>
                    <li>List item two</li>
                    <li>List item three</li>
                </ul>
            </div>
            <h3 class="ui-accordion-header ui-helper-reset ui-state-default ui-accordion-icons ui-corner-top ui-accordion-header-active ui-state-active">
                <span class="ui-accordion-header-icon ui-icon ui-icon-triangle-1-s"></span>
                Section 4
            </h3>
            <div class="ui-accordion-content ui-helper-reset ui-widget-content ui-corner-bottom ui-accordion-content-active">
                <p>
                    Cras dictum. Pellentesque habitant morbi tristique senectus et netus
                    et malesuada fames ac turpis egestas. Vestibulum ante ipsum primis in
                    faucibus orci luctus et ultrices posuere cubilia Curae; Aenean lacinia
                    mauris vel est.
                </p>
                <p>
                    Suspendisse eu nisl. Nullam ut libero. Integer dignissim consequat lectus.
                    Class aptent taciti sociosqu ad litora torquent per conubia nostra, per
                    inceptos himenaeos.
                </p>
            </div>
        </div>
    </div>
</div>

The JavaScript

$(function() {
    $("#accordion > h3").on("click", function(e) {
        $(this).next().slideToggle(function(e) {
            if ($(this).is(":visible")) {
                $(this).addClass("ui-accordion-content-active")
                .prev().toggleClass("ui-corner-all ui-corner-top").addClass("ui-accordion-header-active ui-state-active")
                .children(".ui-accordion-header-icon").toggleClass("ui-icon-triangle-1-e ui-icon-triangle-1-s");
            }
            else {
                $(this).removeClass("ui-accordion-content-active")
                .prev().toggleClass("ui-corner-all ui-corner-top").removeClass("ui-accordion-header-active ui-state-active")
                .children(".ui-accordion-header-icon").toggleClass("ui-icon-triangle-1-e ui-icon-triangle-1-s");
            }
        });
    })
    .hover(function(e) { $(this).toggleClass("ui-state-hover"); });
})

倥絔 2024-09-03 11:56:19

您可以在 jQuery 提供的 document.ready 事件中执行此操作,如下所示:

CSS:

.myClassToBeHidden { display: none; }

jQuery:

$(function() {
  $(".myClassToBeHidden").slideDown();
});

如果您的意思是使用 jQuery UI 中的手风琴控件,那么...您不应该使用手风琴,展开所有内容不是它的用途,他们建议不要这样做,因为它会导致其他问题。

You can do it in the document.ready event jQuery provides, like this:

CSS:

.myClassToBeHidden { display: none; }

jQuery:

$(function() {
  $(".myClassToBeHidden").slideDown();
});

If you mean with the accordion control from jQuery UI then...you shouldn't use accordion, expanding all isn't what it's for and they advise against this as it causes other issues.

不忘初心 2024-09-03 11:56:19

为了使其不引人注目并且仅在访问者有 JavaScript 时隐藏,我将

CSS:

#divToBeHidden { display: block; }

In :

$('#divToBeHidden').hide();

Bottom of :

$(function() {
 $("#divToBeHidden").show(); //Or whatever means you'd prefer of showing the content
});

To make it unobtrusive and be hidden only if the visitor has javascript I'd put

CSS:

#divToBeHidden { display: block; }

In <head>:

$('#divToBeHidden').hide();

Bottom of <body>:

$(function() {
 $("#divToBeHidden").show(); //Or whatever means you'd prefer of showing the content
});
夢归不見 2024-09-03 11:56:19

我知道我正在回答一个 2 年前的问题,但是没有任何解决方法或备用手风琴插件对我有用,所以我想出了一个极其简单的解决方法:只需单击“全部展开”链接即可销毁手风琴,然后单击“全部折叠”链接重新初始化它。像这样的事情:

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

function accordionDestroy() {
    $("#accordion").accordion("destroy");
}

$(function() {
    accordionInit();
});
</script>

展开全部和折叠所有链接将如下所示:

<a onClick="accordionDestroy()">Expand all</a> | <a onClick="accordionInit()">Collapse all</a>

I know I'm answering a 2-year-old question, but none of the workarounds or alternate accordion plugins worked for me, so I came up with a devastatingly simple workaround: just destroy the accordion on click of an "expand all" link, and re-initialize it on click of a "collapse all" link. Something like this:

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

function accordionDestroy() {
    $("#accordion").accordion("destroy");
}

$(function() {
    accordionInit();
});
</script>

The expand all and collapse all links would look like this:

<a onClick="accordionDestroy()">Expand all</a> | <a onClick="accordionInit()">Collapse all</a>
独孤求败 2024-09-03 11:56:19

这个问题问出来已经有9年了。但我找到了以下解决方案来默认打开所有手风琴 div。这里我使用 slideDown() 方法来显示所有 div 内容。

$(function () {
          $("#accordion").accordion({
                header: "> div > h3",
                collapsible: true,
               heightStyle: "content"
           });
          jQuery('#accordion h3.ui-accordion-header').next().slideDown();
 });

It's been 9 years since this question was asked. But I found the below solution to open all the accordion divs by default. Here I used slideDown() method to show all the div contents.

$(function () {
          $("#accordion").accordion({
                header: "> div > h3",
                collapsible: true,
               heightStyle: "content"
           });
          jQuery('#accordion h3.ui-accordion-header').next().slideDown();
 });
各自安好 2024-09-03 11:56:19

我这样做了:

jQuery( function( $ ) {
    $(document).ready(function(){
$( ".accordion" ).accordion({
        animate: 1000, 
        //animated: 'clip',
        autoHeight: true, 
        //collapsible: true,
        //fillSpace: false,
        heightStyle: "content"
    });
    $( ".accordion" ).each(function(index, el) {
        el.active=null
        $(el).find('h3').click(function(event){
            let accordion=$(this).closest(".accordion")
            if(accordion.length){
                if(!accordion[0].active){
                    accordion[0].active=$(this)
                    $(this).closest(".accordion").find('h3').not($(this)).removeClass('ui-accordion-header-active').removeClass('ui-state-active').next().removeClass('ui-accordion-content-active').slideUp()
                }
            }
        })
    });
    $('.accordion h3').addClass('ui-accordion-header-active').addClass('ui-state-active').next().addClass('ui-accordion-content-active').slideDown()
  })
  })
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.13.0/jquery-ui.min.js"></script>
<h1>I will try to improve more</h1>
<div class="accordion">
  <h3>Section 1</h3>
  <div>
    <p>Mauris mauris ante, blandit et, ultrices a, suscipit eget.
    Integer ut neque. Vivamus nisi metus, molestie vel, gravida in,
    condimentum sit amet, nunc. Nam a nibh. Donec suscipit eros.
    Nam mi. Proin viverra leo ut odio.</p>
  </div>
  <h3>Section 2</h3>
  <div>
    <p>Sed non urna. Phasellus eu ligula. Vestibulum sit amet purus.
    Vivamus hendrerit, dolor aliquet laoreet, mauris turpis velit,
    faucibus interdum tellus libero ac justo.</p>
  </div>
  <h3>Section 3</h3>
  <div>
    <p>Nam enim risus, molestie et, porta ac, aliquam ac, risus.
    Quisque lobortis.Phasellus pellentesque purus in massa.</p>
    <ul>
      <li>List item one</li>
      <li>List item two</li>
      <li>List item three</li>
    </ul>
  </div>
</div>

I did this:

jQuery( function( $ ) {
    $(document).ready(function(){
$( ".accordion" ).accordion({
        animate: 1000, 
        //animated: 'clip',
        autoHeight: true, 
        //collapsible: true,
        //fillSpace: false,
        heightStyle: "content"
    });
    $( ".accordion" ).each(function(index, el) {
        el.active=null
        $(el).find('h3').click(function(event){
            let accordion=$(this).closest(".accordion")
            if(accordion.length){
                if(!accordion[0].active){
                    accordion[0].active=$(this)
                    $(this).closest(".accordion").find('h3').not($(this)).removeClass('ui-accordion-header-active').removeClass('ui-state-active').next().removeClass('ui-accordion-content-active').slideUp()
                }
            }
        })
    });
    $('.accordion h3').addClass('ui-accordion-header-active').addClass('ui-state-active').next().addClass('ui-accordion-content-active').slideDown()
  })
  })
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.13.0/jquery-ui.min.js"></script>
<h1>I will try to improve more</h1>
<div class="accordion">
  <h3>Section 1</h3>
  <div>
    <p>Mauris mauris ante, blandit et, ultrices a, suscipit eget.
    Integer ut neque. Vivamus nisi metus, molestie vel, gravida in,
    condimentum sit amet, nunc. Nam a nibh. Donec suscipit eros.
    Nam mi. Proin viverra leo ut odio.</p>
  </div>
  <h3>Section 2</h3>
  <div>
    <p>Sed non urna. Phasellus eu ligula. Vestibulum sit amet purus.
    Vivamus hendrerit, dolor aliquet laoreet, mauris turpis velit,
    faucibus interdum tellus libero ac justo.</p>
  </div>
  <h3>Section 3</h3>
  <div>
    <p>Nam enim risus, molestie et, porta ac, aliquam ac, risus.
    Quisque lobortis.Phasellus pellentesque purus in massa.</p>
    <ul>
      <li>List item one</li>
      <li>List item two</li>
      <li>List item three</li>
    </ul>
  </div>
</div>

药祭#氼 2024-09-03 11:56:19

我知道这确实太晚了,但我今天找到了解决方案。
只需使用

function expandAll() {
$('#accordion h3').removeClass('ui-state-default')
    .addClass('ui-state-active')
    .removeClass('ui-corner-all')
    .addClass('ui-corner-top')
    .attr('aria-expanded', 'true')
    .attr('aria-selected', 'true')
    .attr('tabIndex', 0)
.find('span.ui-icon')
    .removeClass('ui-icon-triangle-1-e')
    .addClass('ui-icon-triangle-1-s')
.closest('h3').next('div')
    .show();}

完整文章的链接

I know that is realy too late but i found the solution today.
Just simply use

function expandAll() {
$('#accordion h3').removeClass('ui-state-default')
    .addClass('ui-state-active')
    .removeClass('ui-corner-all')
    .addClass('ui-corner-top')
    .attr('aria-expanded', 'true')
    .attr('aria-selected', 'true')
    .attr('tabIndex', 0)
.find('span.ui-icon')
    .removeClass('ui-icon-triangle-1-e')
    .addClass('ui-icon-triangle-1-s')
.closest('h3').next('div')
    .show();}

link of full article

酒废 2024-09-03 11:56:19

MultiAccordion jQuery UI 插件对我来说非常有用:
https://anasnakawa.wordpress.com/2011 /01/25/jquery-ui-multi-open-accordion/

在 jQuery UI 参考之后添加插件参考后,您只需要进行 2 个简单的更改:

$("#accordion_div").手风琴(“选项”,“活动”,[0,1]);

MultiAccordion jQuery UI plugin worked great for me:
https://anasnakawa.wordpress.com/2011/01/25/jquery-ui-multi-open-accordion/

After adding the plugin reference after your jQuery UI reference, you just need to make 2 simple changes:

$("#accordion_div").multiAccordion("option", "active", [0,1]);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文