jQuery Jquery手风琴关闭然后打开

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/382301/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 09:04:07  来源:igfitidea点击:

Jquery Accordion Close then Open

jqueryjquery-pluginsaccordion

提问by Jon

I've set up a number of accordions on a page using the jquery accordion plugin so I can implement expand all and collapse all functionality.

我已经使用 jquery 手风琴插件在页面上设置了许多手风琴,因此我可以实现全部展开和折叠所有功能。

Each ID element is it's own accordion and the code below works to close them all no matter which ones are already open:

每个 ID 元素都是它自己的手风琴,无论哪些元素已经打开,下面的代码都可以将它们全部关闭:

$("#contact, #address, #email, #sales, #equipment, #notes, #marketingdata")
    .accordion("activate", -1)
;

My problem is with the expand all. When I have them all expand with this code:

我的问题是全部展开。当我让他们都用这个代码展开时:

$("#contact, #address, #email, #sales, #equipment, #notes, #marketingdata")
    .accordion("activate", 0)
;

Some will contract and some will expand based on whether or not they are previously open.

有些会收缩,有些会根据它们之前是否开放而扩展。

My idea to correct this was to collapse them all and then expand them all when the expand all was clicked. This code however won't execute properly:

我纠正这个问题的想法是将它们全部折叠起来,然后在单击全部展开时展开它们。但是,此代码将无法正确执行:

$("#contact, #address, #email, #sales, #equipment, #notes, #marketingdata")
    .accordion("activate", -1)
;
$("#contact, #address, #email, #sales, #equipment, #notes, #marketingdata")
    .accordion("activate", 0)
; 

It will only hit the second command and not close them all first. Any suggestions?

它只会击中第二个命令,而不是首先关闭它们。有什么建议?

回答by nickf

I'm not exactly sure what you're after, but this is my best guess. Out of all your accordions, you want the "open all" button to open all the accordions which are closed (that is, no section is showing). I'd do that by using filter()

我不确定你在追求什么,但这是我最好的猜测。在您所有的手风琴中,您希望“全部打开”按钮打开所有关闭的手风琴(即不显示任何部分)。我会这样做filter()

$("#contact, #address, #email, #sales, #equipment, #notes, #marketingdata")
    .filter(":not(:has(.selected))")
    .accordion("activate", 0)
;

Is that what you were after?

那是你追求的吗?



Edit to explain that filter function:

编辑以解释该过滤器功能:

The filter function just runs your current selection through a filter, removing anything which doesn't match. It has two different forms: one where you pass a regular jQuery query in, like i did above, and the other where you can define a function to filter. If the function returns false, then that element is removed.

过滤器功能只是通过过滤器运行您当前的选择,删除任何不匹配的东西。它有两种不同的形式:一种是传递常规 jQuery 查询,就像我上面所做的那样,另一种是可以定义要过滤的函数。如果函数返回 false,则删除该元素。

In this case the query removes anything which doesn't (:not) have (:has) a child with class "selected" (.selected). I used the .selectedselector here because that's what the accordion adds to the currently-open panel.

在这种情况下,查询会删除任何没有 ( :not) 具有 ( :has) 类为“selected” ( .selected)的子级的内容。我在.selected这里使用了选择器,因为这是手风琴添加到当前打开面板的内容。

If you only had one accordion, or you gave each of your accordions some sort of identifier, such as a class name, then you could greatly reduce the entire script. Let's say that for each element you want to turn into an accordion, you give it the class "accord".

如果您只有一个手风琴,或者您为每个手风琴都指定了某种标识符,例如类名,那么您可以大大减少整个脚本。假设对于您想要变成手风琴的每个元素,您给它类“accord”。

$(".accord:not(:has(.selected))").accordion("activate", 0);

This is much more legible and maintainable, since you can easily add more accordions in the future if you wish and this will handle it.

这更加清晰和易于维护,因为如果您愿意,将来可以轻松添加更多手风琴,这将处理它。

The documentation for filter is here: http://docs.jquery.com/Traversing/filter

过滤器的文档在这里:http: //docs.jquery.com/Traversing/filter

回答by Sil2

jQuery('#accordion').accordion({        
    active:-1,
});

回答by Sil2

I've had the same issue and resolved it with some short code leveraging JQuery's each() function to collapse while setting focus to item [0] so normal navigation can resume.

我遇到了同样的问题,并通过一些利用 JQuery 的 each() 函数折叠的短代码解决了它,同时将焦点设置为项目 [0],因此可以恢复正常导航。

http://spstring.jeffthink.com/?p=49

http://spstring.jeffthink.com/?p=49