【发布时间】:2010-06-12 00:40:16
【问题描述】:
是否可以通过单独的按钮 onclick 事件在 jquery 手风琴菜单中打开下一个面板?也就是说,不要单击标题打开另一个面板,而是使用未连接到手风琴的按钮。
【问题讨论】:
标签: jquery jquery-ui accordion
是否可以通过单独的按钮 onclick 事件在 jquery 手风琴菜单中打开下一个面板?也就是说,不要单击标题打开另一个面板,而是使用未连接到手风琴的按钮。
【问题讨论】:
标签: jquery jquery-ui accordion
是的,只需像这样在手风琴上调用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);
}
【讨论】: