如何在手风琴中添加+和-

How to add + and - to accordion

本文关键字:添加 手风琴      更新时间:2023-09-26

我已经实现了一个基本的手风琴,我需要在手风琴中添加+和-。

如果手风琴是打开的,那么"-"应该得到其他"+"

这就是尝试过的:

JS:

$('.info').find('.accordion-toggle').click(function () {
    //Expand or collapse this panel
    $(this).next().slideToggle('fast');
    //Hide the other panels
    $(".accordion-content").not($(this).next()).slideUp('fast');
}); 

演示链接

您可以使用css伪元素,如下所示:

CSS:

.accordion-toggle::after {
  content:"+";
}
.accordion-toggle.open::after {
  content:"-";
}

JS:

$('.info').find('.accordion-toggle').click(function () {
  //Expand or collapse this panel
  $(this).toggleClass("open").next().slideToggle('fast');
  //Hide the other panels
  $(".accordion-toggle").not($(this)).removeClass("open");
  $(".accordion-content").not($(this).next()).slideUp('fast');
});

演示