简体   繁体   English

用于在jQuery手风琴上全部展开和折叠的按钮

[英]Button to expand and collapse all on jQuery accordion

I have got a simple jQuery accordion, which requires a button at the top for 'expand/collapse all'. 我有一个简单的jQuery手风琴,它需要在顶部的一个按钮来“全部展开/折叠”。

I've got it working so far, except for when some items are already showing, and the button is clicked, the 'other' items expand/collapse - leaving the already open ones to do the opposite. 到目前为止,我已经使它工作了,除了当某些项目已经显示并且单击按钮时,“其他”项目会展开/折叠-使已经打开的项目相反。

I need to find some sort of reset that will essentially close and re-open all items, and vice-versa. 我需要找到某种重置,从本质上来说,它将关闭并重新打开所有项目,反之亦然。

See code below for accordion and collapse: 有关手风琴和折叠的信息,请参见下面的代码:

<script type="text/javascript">
(function($) {
    $(function() {
        $(".faq-accordion > div").accordion({ header: "h4", collapsible: true, active: false, speed: 10000, });
    })
})(jQuery);
</script>

<script>
$(document).ready(function() {
$('.show-all-applying').click(function() {
    $('.faq-accordion div div').slideToggle("fast");
});
});
</script>

Thank you! 谢谢!

Edit: Here is a JSFiddle showing everything as it is so far (with content changed) 编辑: 这是一个JSFiddle,显示到目前为止的所有内容(内容已更改)

The issues are that I need to: 问题是我需要:

  1. Make the 'show all' button also hide everything once it is clicked for a second time. 再次单击后,使“显示全部”按钮也隐藏所有内容。
  2. The questions when clicked to close sometimes need to be clicked twice, and not sure why. 单击关闭时的问题有时需要单击两次,但不确定为什么。

Dont use slideToggle() , use slideUp() instead ;) 不要使用slideToggle() ,而要使用slideUp() ;)

SlideToggel will slide eveything that is up down and everything that is down up. SlideToggel会滑动向上和向下的所有东西。

You want everything to slide up so use slideUp() 您希望所有内容都向上滑动,因此请使用slideUp()

EDIT : I modified your jsfiddle, and got it working. 编辑 :我修改了您的jsfiddle,并使其工作。 When a user clicks the button 'show all' it will check what the text is. 当用户单击“全部显示”按钮时,它将检查文本是什么。 So in this case 'show all' and it will all slideDown. 因此,在这种情况下,“显示全部”,所有内容都将下滑。 It then changes the text to 'hide all'. 然后,它将文本更改为“全部隐藏”。 If the user clicks on 'hide all', it will all slideUp. 如果用户单击“全部隐藏”,它将全部滑动。 If you use slideToggle in this case, it will work also BUT if a user clicks on a question title, and it opens and then clicks on the show all it will CLOSE the question title that was clicked on and open all the others! 如果在这种情况下使用的slideToggle,它也将工作, 如果用户点击一个问题的标题,和它打开,然后点击了显示所有,将关闭被点击的问题标题和打开所有的人!

WORKING FIDDLE 工作场所

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

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