通过活动触发jquery手风琴菜单?
是否可以通过单独的按钮onclick事件打开jquery手风琴菜单中的下一个面板? 这不是单击标题打开另一个面板,而是使用未连接到手风琴的按钮。
是的,只需在手风琴上调用activate
,如下所示:
$("#myaccordion").accordion("activate", 1 );
其中1
是您要打开的索引。
您可以通过调用以下命令获取活动面板的当前从零开始的索引:
var index = $("#myaccordion").accordion('option','active');
因此,将这两个项目放在一起,我们可以点击一下打开下一个项目:
$("#mybutton").click(function(e){ e.preventDefault(); var acc = $("#myaccordion"), index = acc.accordion('option','active'), total = acc.children('div').length, nxt = index + 1; if (nxt >= total) { nxt = 0; // Loop around to the first item } acc.accordion('activate', nxt); })
在JQuery UI 1.10或更高版本的版本中,.activate函数已被弃用,转而使用’option’方法,因此使用前一个答案的替代方法将是:
$("#button").click(function(){ var index = $("#accordion").accordion('option','active'); var total = $("#accordion").children('div').length; index++; // include restart same as previous answer if (index >= total) { index = 0; } $("#accordion").accordion("option", "active", index); }