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

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

问题描述

我使用 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 选择器,因为这是手风琴添加到当前打开的面板的内容.

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 Accordion 关闭然后打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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