简体   繁体   English

如何使用Adi Palaz的嵌套手风琴展开全部/全部折叠

[英]How to Expand All/Collapse All Using Adi Palaz's Nested Accordion

Adi Palaz's Nested Accordion Adi Palaz的嵌套手风琴

This seems like it should be simple but I can't seem to figure this out and I have been sitting here slamming my head on my desk after like four hours without a solution. 这似乎很简单,但我似乎无法弄清楚,我坐在这里就像在四个小时后没有解决办法一样,把头砸在桌子上。

You will notice in the demo on the page that there are expand all/collapse all buttons that fire a function to open all the accordion panels or close them. 您会在页面上的演示中注意到,有全部展开/全部折叠按钮可以触发打开或关闭所有手风琴面板的功能。

I DON'T want to use those buttons. 我不想使用这些按钮。 I want to write my own function and fire the expand all or collapse all function after the user completes a gesture on a DIV somewhere else on the page. 我想编写自己的函数,并在用户在页面上其他位置的DIV上完成手势后触发全部展开或折叠全部功能。

But I can't seem to figure out how to call the same function the author is using on the buttons to properly expand and collapse the accordion panels. 但是我似乎无法弄清楚如何调用作者在按钮上使用的相同功能来正确展开和折叠手风琴面板。

If it helps, I set up a test page to play with: http://dl.dropbox.com/u/22224/Newfolder/nested_accordion_demo3.html 如果有帮助,我将设置一个测试页面以供使用: http : //dl.dropbox.com/u/22224/Newfolder/nested_accordion_demo3.html

And here are the two scripts it needs to work: 这是它需要工作的两个脚本:

Nested Accordion Script 嵌套手风琴脚本

Expand.js Expand.js

Please help! 请帮忙! I am desperate and the author is not responding to emails! 我很拼命,作者没有回复电子邮件!

I was able to solve the expand/collapse all problem with the following code, hope it will work for you as well. 我可以使用以下代码解决扩展/折叠所有问题,希望它也对您有用。

function expand(id) {
    var o = $.extend({}, $.fn.accordion.defaults, null);
    var containerID = o.container ? id : '', objID = o.objID ? o.objID : o.obj + o.objClass, Obj = o.container ? containerID + ' ' + objID : id, El = Obj + ' ' + o.el, hTimeout = null;
    $(El + ' a.trigger').closest(o.wrapper).find('> ' + o.next).show().closest(o.wrapper).find('a.trigger').addClass('open').data('state', 1);
}

function collapse(id) {
    var o = $.extend({}, $.fn.accordion.defaults, null);
    var containerID = o.container ? id : '', objID = o.objID ? o.objID : o.obj + o.objClass, Obj = o.container ? containerID + ' ' + objID : id, El = Obj + ' ' + o.el, hTimeout = null;
    $(El + ' a.trigger').closest(o.wrapper).find('> ' + o.next).not('.shown').hide().closest(o.wrapper).find('a.open').removeClass('open').data('state', 0);
}

Example:

expand('#accordion1');
collapse('#accordion1');

I'm faced with the exact same problem and would like to know if you ever found an answer? 我面临着完全相同的问题,想知道您是否找到答案了吗?

The work around I plan to use is something like $('.accordion a').click(); 我计划使用的解决方法类似于$('。accordion a')。click();。 to programmatically click each link in the list - not pretty but it seems to work... 以编程方式单击列表中的每个链接-不太漂亮,但似乎可以使用...

Two years later and I have the same desire... use Adi Palaz nested accordion, but have my OWN styled and specific action expand/collapse all buttons. 两年后,我有同样的愿望...使用Adi Palaz嵌套手风琴,但要用我自己的OWN风格和特定的动作来扩展/折叠所有按钮。 I did finally get it to work the way I wanted even though I am admittedly a novice level jquery programmer. 即使我确实是菜鸟级的jquery程序员,我也终于按照我想要的方式工作了。 Here's my key learnings: 这是我的主要知识:

  • I started with example nested_accordion_demo5.html with the #acc1 example. 我从带有#acc1示例的nested_accordion_demo5.html示例开始。 I did not use expand.js at all, I never could get it to work. 我根本没有使用expand.js,我永远无法使它工作。
  • I changed the function defaults in my demo5.html to add the obj: "div". 我在demo5.html中更改了函数默认值,以添加obj:“ div”。

  $("#acc1").accordion({ obj: "div", el: ".h", head: "h4, h5", next: "div", iconTrigger: true }); 

  • Then I added a div with class="accordion" around the whole ul structure and removed the accordion class from the ul. 然后,我在整个ul结构周围添加了带有class =“ accordion”的div,并从ul中删除了手风琴类。
  • I made my own two expand/collapse links and put them inside the div but before the ul. 我创建了自己的两个展开/折叠链接,并将它们放在div中但在ul之前。 Later I got fancy and added styling to make them look like buttons, but first I got it working. 后来我很喜欢并添加了样式使它们看起来像按钮,但是首先我使它起作用。

 <a href="#" class="ExpandAll">[Expand All]</a>&nbsp;<a href="#" class="CollapseAll">[Collapse All]</a> 

  • Then I added two new separate event handlers to jquery.nestedAccordion.js using snippets from the one that handles the a.trigger events. 然后,我使用处理a.trigger事件的代码片段中的片段向jquery.nestedAccordion.js添加了两个新的单独事件处理程序。 I placed them immediately after the existing event handler. 我将它们放在现有事件处理程序之后。 Here's my new CollapseAll: 这是我的新CollapseAll:

 $(Obj).delegate('a.CollapseAll', ev, function(ev) { $( o.obj + o.objClass ).find('a.trigger').each(function() { var $thislink = $(this), $thisWrapper = $thislink.closest(o.wrapper), $nextEl = $thisWrapper.find('> ' + o.next); if (($nextEl).length && $thislink.data('state') && (o.collapsible)) { $thislink.removeClass('open'); $nextEl.filter(':visible')[o.hideMethod](o.hideSpeed, function() {$thislink.data('state', 0);}); } }); }); 

  • Then I made a second event handler that does the ExpandAll. 然后,我创建了另一个执行ExpandAll的事件处理程序。

I know this could likely be much more efficient, but I am thrilled to at least have it working given my skill level! 我知道这可能会更有效率,但是我很高兴至少在我的技能水平下可以正常工作!

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

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