css jQuery展开/折叠(带标题)

1wnzp6jl  于 2023-02-26  发布在  jQuery
关注(0)|答案(3)|浏览(211)

我尝试过使用accordion插件,但它不起作用,我知道使用纯jQuery有一个更简单的解决方案。
从本质上讲,我希望当你用class="expand"单击一个<h2>时,它应该用class="collapse"“展开”下一个div。
任何帮助将不胜感激,提前感谢!
有关测试目的,请参阅此jsFiddle演示。

bqjvbblv

bqjvbblv1#

const $headers = $('.header');
const $contents = $('.content');

$headers.on("click", function() {
 const $cont = $(this).next(".content");   
 $contents.not($cont).slideUp();    // Hide all
 $cont.slideToggle();               // Toggle one
});
.header { background: #ddd; cursor: pointer; margin: 0; }
.content{ display:none; }
<h2 class="header">Click to expand and collapse</h2>
<div class="content">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean bibendum dolor nec nunc aliquam placerat. Nullam vehicula nibh felis. Nulla tincidunt aliquam nisl nec sagittis. Donec convallis hendrerit nisl, ut lacinia elit sagittis a. Nullam sollicitudin
  ultricies nibh, tincidunt adipiscing erat tristique vitae. Sed id ipsum ac ipsum fringilla molestie et sit amet elit. Cras commodo augue id dolor suscipit commodo. Ut varius porta orci, quis dignissim ante adipiscing et. Pellentesque rhoncus purus ut
  tortor tempus auctor.</div>

<h2 class="header">Click to expand and collapse 2</h2>
<div class="content">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean bibendum dolor nec nunc aliquam placerat. Nullam vehicula nibh felis. Nulla tincidunt aliquam nisl nec sagittis. Donec convallis hendrerit nisl, ut lacinia elit sagittis a. Nullam sollicitudin
  ultricies nibh, tincidunt adipiscing erat tristique vitae. Sed id ipsum ac ipsum fringilla molestie et sit amet elit. Cras commodo augue id dolor suscipit commodo. Ut varius porta orci, quis dignissim ante adipiscing et. Pellentesque rhoncus purus ut
  tortor tempus auctor.</div>

<h2 class="header">Click to expand and collapse 3</h2>
<div class="content">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean bibendum dolor nec nunc aliquam placerat. Nullam vehicula nibh felis. Nulla tincidunt aliquam nisl nec sagittis. Donec convallis hendrerit nisl, ut lacinia elit sagittis a. Nullam sollicitudin
  ultricies nibh, tincidunt adipiscing erat tristique vitae. Sed id ipsum ac ipsum fringilla molestie et sit amet elit. Cras commodo augue id dolor suscipit commodo. Ut varius porta orci, quis dignissim ante adipiscing et. Pellentesque rhoncus purus ut
  tortor tempus auctor.</div>
  
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

它在收拢关闭的图元之前隐藏所有打开的图元。
如果元素是打开的,它将切换打开状态。

kuuvgm7e

kuuvgm7e2#

这很简单,只需使用下面的

$('.expand').click(function() {
  $(this).next('.collapse').slideToggle(); // or use .toggle() for no animation
});

小提琴:http://jsfiddle.net/garreh/WQYc7/2/

要默认折叠,只需添加以下css:

.collapse {
    display: none;
}
eit6fx6z

eit6fx6z3#

使用jQuery,我已经更新了你的Fiddle。

    • 超文本标记语言**:
<div id="accordion">
<h2 class="toggle">Click to expand and collapse</h2>

<div class="pane">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean bibendum dolor nec nunc aliquam placerat. Nullam vehicula nibh felis. Nulla tincidunt aliquam nisl nec sagittis. Donec convallis hendrerit nisl, ut lacinia elit sagittis a. Nullam sollicitudin ultricies nibh, tincidunt adipiscing erat tristique vitae. Sed id ipsum ac ipsum fringilla molestie et sit amet elit. Cras commodo augue id dolor suscipit commodo. Ut varius porta orci, quis dignissim ante adipiscing et. Pellentesque rhoncus purus ut tortor tempus auctor.
</div>

<h2 class="toggle">Click to expand and collapse 2</h2>
<div class="pane"><br>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean bibendum dolor nec nunc aliquam placerat. Nullam vehicula nibh felis. Nulla tincidunt aliquam nisl nec sagittis. Donec convallis hendrerit nisl, ut lacinia elit sagittis a. Nullam sollicitudin ultricies nibh, tincidunt adipiscing erat tristique vitae. Sed id ipsum ac ipsum fringilla molestie et sit amet elit. Cras commodo augue id dolor suscipit commodo. Ut varius porta orci, quis dignissim ante adipiscing et. Pellentesque rhoncus purus ut tortor tempus auctor.
</div>
</div>
    • Javascript**:
<script>
$(document).ready(function() {
  $('#accordion h2').click(function() {
    var $nextDiv = $(this).next();
    var $visibleSiblings = $nextDiv.siblings('div:visible');

    $(this).toggleClass('current').siblings('h2').removeClass('current');
    if ($visibleSiblings.length ) {
      $visibleSiblings.slideUp('fast', function() {
      $nextDiv.slideToggle('fast');
      });
    } else {
       $nextDiv.slideToggle('fast');
    }
  });
});​
</script>
    • 中央支助组:**
.toggle { 
    background: gray;
    padding: 5px;
    cursor: pointer;
}
.pane { display: none; }
​.current { background: green }

相关问题