简体   繁体   English

JavaScript按属性对所有其他子元素进行排序,但(this)

[英]JavaScript order all other child elements by attribute but (this)

I don't want to clutter this question with code. 我不想用代码弄乱这个问题。 So here's my fiddle. 所以这是我的小提琴。 http://jsfiddle.net/jaisfiddles/acdk1aLL/5/ http://jsfiddle.net/jaisfiddles/acdk1aLL/5/

And here is embedded demo: 这是嵌入式演示:

 $(document).ready(function() { $(".tabs").on('click', function() { $(this).parent().prepend($(this)); var childDivs = $("div.tabs"); for (var i = 1;i<childDivs.length;i++){ $(childDivs[i]).addClass('sort'); } }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="body"> <div id="vert-navbar"> <div id="tab1" class="tabs"><p>TAB1</p></div> <div id="tab2" class="tabs"><p>TAB2</p></div> <div id="tab3" class="tabs"><p>TAB3</p></div> <div id="tab4" class="tabs"><p>TAB4</p></div> </div> <div id="main-content"></div> </div> 

The intention is to move a clicked-on DIV to top and sort all other child DIVs in alphabetical order. 目的是将单击的DIV移到顶部并按字母顺序对所有其他子DIV进行排序。 Eg. 例如。 If TAB 3 is clicked on, move TAB 3 to top. 如果单击了TAB 3,则将TAB 3移到顶部。 And reorder the rest as TAB1, TAB2, TAB4. 并将其余部分重新排序为TAB1,TAB2,TAB4。 I've managed to bring the selection to the top using 我设法使用

    $(this).parent().prepend($(this));

This may seem sorted but not really. 这看起来似乎是分类的,但不是真的。

So, I thought I'd create an array of elements (starting at 1 not 0 to get all elements starting with the second one) by assigning them a classname 'sort' as you'll see on the fiddle. 因此,我以为我会创建一个元素数组(从1而不是0开始,使所有元素从第二个元素开始),方法是为它们分配一个类名'sort',就像在小提琴中看到的那样。 And sort this array and append the parent DIV - vert-navbar. 并对这个数组进行排序,并附加父DIV-vert-navbar。

You need to sort first and then append. 您需要进行排序,然后追加。 This way it is guarantied that the order is correct after you move clicked item to the top: 这样可以保证将单击的项目移到顶部后顺序是正确的:

 var $tabs = $(".tabs").on('click', function () { var $parent = $(this).parent(); $tabs.sort().appendTo($parent); $parent.prepend($(this)); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="body"> <div id="vert-navbar"> <div id="tab1" class="tabs"><p>TAB1</p></div> <div id="tab2" class="tabs"><p>TAB2</p></div> <div id="tab3" class="tabs"><p>TAB3</p></div> <div id="tab4" class="tabs"><p>TAB4</p></div> </div> <div id="main-content"></div> </div> 

You can first reset the block arrangement on each click event then just prepend the clicked div to the parent 您可以先在每次点击事件上重置屏蔽排列,然后将被点击的div放在父项之前

$(document).ready(function() {
    $(".tabs").on('click', function() {
        reset();
        $(this).parent().prepend($(this));

    });
});
function reset(){
    var childDivs = $("div.tabs");
    for (var i = childDivs.length;i>0;i--){
        $("#vert-navbar").prepend($("#tab"+i));
    }
}

check the fiddle 检查小提琴

    $(document).ready(function() {
        $(".tabs").on('click', function() {
            $(this).parent().prepend($(this));
            var orderlist = $("#vert-navbar .tabs").slice(1).sort(function(a,b){
                if($(a).find("p").text()<$(b).find("p").text()){
                    return -1;
                }
                else if($(a).find("p").text()>$(b).find("p").text()){
                    return 1;
                }
                return 0;
            });
            orderlist.splice(0,0,$("#vert-navbar .tabs").eq(0)[0]);
            $("#vert-navbar").append(orderlist);
        });
    });

Demo: http://jsfiddle.net/acdk1aLL/6/ 演示: http//jsfiddle.net/acdk1aLL/6/

Reorder the rest of the list and then add new first element. 重新排列列表的其余部分,然后添加新的第一个元素。

You can use the sort method on the remainder of the tabs and then append them to the parent of the clicked element as follows. 您可以在其余选项卡上使用sort方法,然后append它们append到clicked元素的父级,如下所示。

 $(document).ready(function() { $(".tabs").on('click', function() { $(this).parent().prepend( $(this) ) .append( $("div.tabs").not( this ).sort(function(a,b) { return $('p',a).text() > $('p',b).text() ? 1 : $('p',b).text() > $('p',a).text() ? -1 : 0; }) ); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="body"> <div id="vert-navbar"> <div id="tab1" class="tabs"><p>TAB1</p></div> <div id="tab2" class="tabs"><p>TAB2</p></div> <div id="tab3" class="tabs"><p>TAB3</p></div> <div id="tab4" class="tabs"><p>TAB4</p></div> </div> <div id="main-content"></div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM