简体   繁体   English

jQuery ui手风琴 - 不同的折叠面板?

[英]jQuery ui accordion - different collapse panel?

I couldn't quite find what I was looking for on other questions so here goes. 我在其他问题上找不到我想要的东西,所以这里就是这样。

Here is my code for an accordion using jQuery UI 这是我使用jQuery UI的手风琴代码

    $('.blog-m-post').accordion({
      header: "h3",
      active: "true"
    });

My problem is that the docs state 我的问题是文档说明了

Content panels must be the sibling immediately after their associated headers. 内容面板必须是其关联标头后的兄弟。

But with my markup, the content panel isn't immediately after the header, but one after that. 但是使用我的标记,内容面板不会立即在标题之后,而是在之后。

Example: 例:

<div class="blog-m-post">
  <h3>My blog post title</h3>

  <div class="foo"> <!-- this becomes the collapsed element -->
    <p>Posted on: 8th Aug</p> <p>Author: John Doe</p>
  </div>

  <div class="content"> <!-- but I want THIS to collapse instead -->
    <p>my content inside here</p>
  </div>
</div>

How can I make the .content be the collapsable element as currently, it is .foo ? 如何使.content成为当前的可折叠元素,它是.foo

The .foo needs to stay where it is in terms of ordering as it needs to be shown by default. .foo 需要保持在排序的位置,因为它需要默认显示。 .content is where the inner content will be that should collapse and open. .content是应该折叠和打开的内部内容。

You can put <div class="foo"></div> inside a HEADER-div and keep the <div class="content"> as immediate sibling of that HEADER. 您可以将<div class="foo"></div>放在HEADER-div中,并将<div class="content">保持为该HEADER的直接兄弟。

For example: 例如:

 <div class="blog-m-post">
      <div class="header">
        <h3>My blog post title</h3>
        <div class="foo"> <!-- this becomes the collapsed element -->
          <p>Posted on: 8th Aug</p> <p>Author: John Doe</p>
        </div>
      </div>

      <div class="content"> <!-- but I want THIS to collapse instead -->
        <p>my content inside here</p>
      </div>
 </div>

and the JQuery would be: 而JQuery将是:

$('.blog-m-post').accordion({
      header: "div.header",
      active: "true"
});

Here is the JsFiddle 这是JsFiddle

As docs state: 正如文档所述

The markup of your accordion container needs pairs of headers and content panels 您的手风琴容器的标记需要成对的标题和内容面板

and also (as you already know) 而且(正如你所知)

Content panels must be the sibling immediately after their associated headers. 内容面板必须是其关联标头后的兄弟。

So you have to pair each header with the associated content to make it/them collapsible. 因此,您必须将每个标头与关联的内容配对,以使其/可折叠。 Having an extra element inside the accordion and always visible requires quite a hack and I would suggest using toggle instead. 在手风琴中有一个额外的元素并且始终可见需要非常黑客,我建议使用切换。 Anyway, in accordion a solution would be to append the extra content after the creation and when activate remove it, refresh and append again. 无论如何,在手风琴中,解决方案是在创建后附加额外内容,并在激活时删除它,刷新并再次追加。 Ultimately, something like that: 最终,类似的东西:

 $('.blog-m-post').accordion({ header: "h3", active: "true", collapsible: true, create: function(){ $('h3').after('<div class="extra content ui-accordion-content ui-helper-reset ui-widget-content ui-corner-bottom ui-accordion-content-active"><div class="foo">Foo content here</div></div>'); }, activate: function( event, ui ) { $('.extra').remove(); $( ".blog-m-post" ).accordion( "refresh" ); if(ui.newHeader.length==1){//open $('.foo').remove(); $('.content').prepend('<div class="foo">Foo content here</div>') }else{ $('h3').after('<div class="extra content ui-accordion-content ui-helper-reset ui-widget-content ui-corner-bottom ui-accordion-content-active"><div class="foo">Foo content here</div></div>'); } } }); 
 <script src="https://code.jquery.com/jquery-2.2.4.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script> <link href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css" rel="stylesheet"/> <div class="blog-m-post"> <h3>Accordion</h3> <div class="content"> <p>my content inside here</p> </div> </div> 

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

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