Jquery手风琴关闭然后打开 [英] Jquery Accordion Close then Open

查看:35
本文介绍了Jquery手风琴关闭然后打开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

$("#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?

推荐答案

我不太确定您想要什么,但这是我最好的猜测.在您所有的手风琴中,您希望全部打开"按钮打开所有关闭的手风琴(即不显示任何部分).我会通过使用 filter()

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()

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

这就是你所追求的吗?

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

Edit to explain that filter function:

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

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.

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

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

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

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: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.

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

这篇关于Jquery手风琴关闭然后打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆