Jquery 手风琴关闭然后打开

发布于 2024-07-09 21:31:50 字数 804 浏览 10 评论 0原文

我使用 jquery 手风琴插件在页面上设置了多个手风琴,这样我就可以实现全部展开和折叠所有功能。

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

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

我的问题是全部展开。 当我用这段代码让它们全部展开时:

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

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

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

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

它只会执行第二个命令,而不会首先关闭所有命令。 有什么建议么?

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.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

墨小沫ゞ 2024-07-16 21:31:50

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

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

这就是你想要的吗?


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

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

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

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

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

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

过滤器的文档位于:http://docs.jquery.com/Traversing/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)
;

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.

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.

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.

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

手长情犹 2024-07-16 21:31:50
jQuery('#accordion').accordion({        
    active:-1,
});
jQuery('#accordion').accordion({        
    active:-1,
});
灼痛 2024-07-16 21:31:50

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

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

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.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文