【问题标题】:jQuery sorting divs within two columnsjQuery 对两列中的 div 进行排序
【发布时间】:2013-02-21 15:50:06
【问题描述】:

我有一个包含两列的主列表。在这些列中,每个 div 都有多个类,当您选择其中一个类时,我想对列进行排序,使它们均匀。这是我开始的一个例子:

<div class="left">
  <div class="dot blue">blue one</div>
  <div class="dot red">red one</div>
  <div class="dot orange">orange one</div>
  <div class="dot red">red two</div>
  <div class="dot red">red three</div>
</div>
<div class="right">
  <div class="dot red">red four</div>
  <div class="dot blue">blue two</div>
  <div class="dot blue">blue three</div>
  <div class="dot blue">blue four</div>
  <div class="dot orange">orange two</div>
</div>

单击“红色”的切换按钮后,我想隐藏除红点之外的所有内容,但在两列之间均匀地对红色进行排序。我可以正确隐藏所有内容,但不确定如何在“左”和“右” div 之间制作四个红色 div。这是我想要的输出:

<div class="left">
  <div class="dot red">red one</div>
  <div class="dot red">red two</div>
</div>
<div class="right">
  <div class="dot red">red three</div>
  <div class="dot red">red four</div>
</div>

提前致谢。

【问题讨论】:

    标签: jquery html multiple-columns


    【解决方案1】:

    将所有.reds 向左移动,然后找到中间点,将组的后半部分向右移动:

    $('.left,.right').find('.dot:not(.red)').hide(); /* or .remove() */
    var $red = $('.left,.right').find('.dot.red').appendTo('.left');
    var len = Math.round($red.length/2);
    $('.left .dot.red:gt('+(len-1)+')').appendTo('.right'); /* :gt is zero-based */
    

    http://jsfiddle.net/mblase75/nnhBp/

    http://api.jquery.com/gt-selector

    【讨论】:

      【解决方案2】:

      你可以使用

      $(function(){
          var dots = $('.dot'),
              left = $('.left'),
              right = $('.right');
      
          $('button').on('click', function(){
              var filter = $(this).data('filter'); // here you define which class to remain (on demo i have added a data-filter attribute on the buttons)
              var filtered = dots.detach().filter('.' + filter),
                  half = Math.ceil(filtered.length / 2);
      
              filtered.each(function(index){
                  var target = left;
                  if (index >= half){
                      target = right;
                  }
                  $(this).appendTo( target );
              });
      
          });
      });
      

      演示地址 http://jsfiddle.net/hnaUK/1/

      【讨论】:

        【解决方案3】:

        我知道你有一些选择,但我研究了一段时间,并想我也会分享我的:

        这是我的 jQuery:

            $(document).ready(function () {
                $("button").click(function () {
                    var classVal = "." + $(this).val();
        
                    $(".dot").hide();
                    $(classVal).show();
        
                    var halfOf = ($(classVal).length / 2)-1;
        
                    $(classVal).appendTo(".left");
                    $(classVal+":gt('"+halfOf+"')").appendTo(".right");
                });                
            });
        

        这是 HTML,都是一样的,但我必须添加你提到的按钮:

        <div class="left">
            <div class="dot blue">blue one</div>
            <div class="dot red">red one</div>
            <div class="dot orange">orange one</div>
            <div class="dot red">red two</div>
            <div class="dot red">red three</div>
        </div>
        <div class="right">
            <div class="dot red">red four</div>
            <div class="dot blue">blue two</div>
            <div class="dot blue">blue three</div>
            <div class="dot blue">blue four</div>
            <div class="dot orange">orange two</div>
        </div>
        <div id="controls">
            <button value="red" type="button">Red</button>
            <button value="blue" type="button">Blue</button>
            <button value="orange" type="button">Orange</button>
        </div>
        

        然后是一些简单的 CSS:

        .left {
            float: left;
            border: 1px solid #000000;
        }
        .right {
            float: left;
            border: 1px solid #000000;
        }
        #controls {
            clear: both;
        }
        

        随着小提琴:

        http://jsfiddle.net/CTCPZ/

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-02-08
          • 2023-03-31
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多