通过活动触发jquery手风琴菜单?

Edw*_*ard 10 jquery jquery-ui accordion

是否可以通过单独的按钮onclick事件打开jquery手风琴菜单中的下一个面板?这不是单击标题打开另一个面板,而是使用未连接到手风琴的按钮.

Dou*_*ner 14

是的,只需activate按下这样的手风琴:

$("#myaccordion").accordion("activate", 1 );
Run Code Online (Sandbox Code Playgroud)

1您要打开的索引在哪里.

您可以通过调用以下方法获取活动面板的当前从零开始的索引:

var index = $("#myaccordion").accordion('option','active');
Run Code Online (Sandbox Code Playgroud)

因此,将这两个项目放在一起,我们可以点击一下打开下一个项目:

$("#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);
})
Run Code Online (Sandbox Code Playgroud)


Cra*_*ino 14

在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);

}
Run Code Online (Sandbox Code Playgroud)