如何使用jQuery正确排序多个手风琴

lok*_*pse 6 jquery jquery-ui accordion jquery-ui-sortable

这是我第一次发帖,所以如果我没有提供足够的细节,请告诉我.

我有一个包含多个手风琴的页面.我想使用多种手风琴,因为我希望用户能够同时打开多个部分,这对于手风琴来说不是原生的.

我也希望用户能够对那些手风琴进行排序.当前页面的设置方式,用户可以毫无问题地对手风琴进行排序.

当用户对手风琴进行排序时,我有一个脚本将关闭所有现有的手风琴,将它们的ID存储在一个数组中,然后在用户完成排序时重新打开它们.那部分也很好.

当您尝试将要排序的部分放在先前打开的两个部分之间时,会发生此问题.似乎jQuery不能轻易地告诉一个部分开始的位置,并且一个部分以编程方式关闭时结束.请注意,如果它们已经关闭,它会按预期工作.

我已经尝试了几种解决这个问题的方法,包括在部分之间添加一个间隔div以及破坏手风琴,然后在分类完成后重新初始化它们无济于事.

我怀疑问题出现了,因为当第一次抓住手风琴时,这些部分是开放的.但是在那一点上,我关闭所有部分,即使该部分关闭,当你试图在两个部分之间放置一个部分时,它会导致这些部分偶尔跳转.

我还注意到手风琴内容的高度因素.在下面的例子中,第一个div在打开时会引起问题而你试图对它们进行排序,但其他不那么.

这是我的代码.jQuery是基本的jQuery库,我编写的内容包括inline以简化:

<html>
<head>
<link href="/css/flick/jquery-ui.css" media="screen" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="/js/jquery.js"></script>
<script type="text/javascript" src="/js/jquery-ui.js"></script>
<script type="text/javascript">
  $(document).ready(function() {
    // Add Accordion stuff
    $(".accordion").accordion({
      autoHeight: false,
      active: false,
      collapsible: true
    }); // end $(".accordion").accordion

    // Add sortables
    $('.sortable').sortable({
      start: function(e, ui) {
        container = $(e.target);

        var parent_id = container.parent().parent().attr('id');
        expanded_ones = new Array();
        var count = 0;
        var summary = '';
        var child = '';
        var active = '';

        // now close all other sections
        $.each($('#' + parent_id + ' .accordion'), function() {
          // get the child element since that has the div id I need
          child = $(this).children('div');

          // get the active information to see if it is open or closed
          active = $(this).accordion('option', 'active');

          // check to see if this one is expanded
          if(parseInt(active) == active) {
            // store this id so we can open it later
            expanded_ones[count] = $(child).attr('id');
            count++;

            // and close the accordion
            $(this).accordion({ active: false });
          } // end if(parseInt(active) == active)
        }); // end $.each($('#' + parent_id + ' .accordion'), function()
      }, // end start: function(e, ui)
      stop: function(e, ui) {
        container = $(e.target);

        var parent_elem = '';

        // expand the ones that were originally expanded
        for(var i = 0; i < expanded_ones.length; i++) {
          parent_elem = $('#' + expanded_ones[i]).parent();
          $(parent_elem).accordion('option', 'active', 0);
        } // end for(var i = 0; i < expanded_ones; i++)
      } // end stop: function(e, ui)
    }); // end $('.sortable').sortable
  }); // end $(document).ready(function() {
</script>
</head>
<body>
<div id="outer">
  <div class="box">
    <div class="accordion_wrapper sortable" rel="sections">
      <div id="accordion_a" class="section_accordion">
        <div class="accordion">
          <h4>Accordion A</h4>
          <div id="accordion_a_content">
            <p>Stuff</p>
            <p>Stuff</p>
            <p>Stuff</p>
            <p>Stuff</p>
            <p>Stuff</p>
            <p>Stuff</p>
            <p>Stuff</p>
            <p>Stuff</p>
            <p>Stuff</p>
            <p>Stuff</p>
            <p>Stuff</p>
            <p>Stuff</p>
            <p>Stuff</p>
            <p>Stuff</p>
            <p>Stuff</p>
            <p>Stuff</p>
            <p>Stuff</p>
            <p>Stuff</p>
            <p>Stuff</p>
            <p>Stuff</p>
            <p>Stuff</p>
            <p>Stuff</p>
            <p>Stuff</p>
            <p>Stuff</p>
            <p>Stuff</p>
            <p>Stuff</p>
            <p>Stuff</p>
            <p>Stuff</p>
            <p>Stuff</p>
            <p>Stuff</p>
            <p>Stuff</p>
            <p>Stuff</p>
            <p>Stuff</p>
            <p>Stuff</p>
            <p>Stuff</p>
            <p>Stuff</p>
          </div>
        </div>
      </div>

      <div id="accordion_b" class="section_accordion">
        <div class="accordion">
          <h4>Accordion B</h4>
          <div id="accordion_b_content">
            Stuff
          </div>
        </div>
      </div>

      <div id="accordion_c" class="section_accordion">
        <div class="accordion">
          <h4>Accordion C</h4>
          <div id="accordion_c_content">
            Stuff
          </div>
        </div>
      </div>

      <div id="accordion_d" class="section_accordion">
        <div class="accordion">
          <h4>Accordion D</h4>
          <div id="accordion_d_content">
            Stuff
          </div>
        </div>
      </div>
    </div>
  </div>
    </div>
  </body>
</html>
Run Code Online (Sandbox Code Playgroud)

Tim*_*hle 2

首先我想说,这真的需要手风琴或可排序吗?其次,我在这里设置了你的例子。使用此示例,可排序可以将页面发送到各处,尤其是当手风琴 A 打开时。这可能会让一些用户感到非常困惑。我建议寻找另一种选择来完成您的任务。

来自Accordian 上的jQueries 网站API

注意:如果您想一次打开多个部分,请不要使用手风琴

手风琴不允许同时打开多个内容面板,并且需要付出很大的努力才能做到这一点。如果您正在寻找一种允许打开多个内容面板的小部件,请不要使用它。通常它可以用几行 jQuery 来编写,如下所示:

 $(document).ready(function(){
    $('.accordion .head').click(function(){
        $(this).next().toggle();
        return false;   
    }).next().hide(); 
  });
Run Code Online (Sandbox Code Playgroud)

或者动画:

 $(document).ready(function(){
    $('.accordion .head').click(function() {
        $(this).next().toggle('slow');
        return false;
     }).next().hide();
 });
Run Code Online (Sandbox Code Playgroud)

因此,如果您想打开多个可排序的手风琴,我建议您创建自己的自定义代码。