簡體   English   中英

jQuery-使用data()按類對div排序

[英]Jquery - Sort div by class using data()

是否可以通過使用data函數對div進行排序?

html

<div id="gp_21" class="line">one</div>
<div id="gp_35" class="line">two</div>
<div id="gp_11" class="line">three</div>
<div id="gp_29" class="line">four</div>

<hr />

<div id="check"></div>

js

$(document).ready(function rt() {

    $('#gp_21').data("rtt", { age: '251351' });
    $('#gp_35').data("rtt", { age: '25131151' });
    $('#gp_11').data("rtt", { age: '251' });
    $('#gp_29').data("rtt", { age: '25131148' });

check();

});


function check()
{

    $('.line').each(function() {

     age = $('#'+this.id+'').data("rtt").age;

     $('#check').append('-> '+age+' - '+this.id+'<br />');


    });

}

工作示例-> http://www.jsfiddle.net/V9Euk/265/

提前致謝! 彼得

嗨看到這個http://www.jsfiddle.net/V9Euk/268/

function check()
{
    var ages= [];
    var ids = []
    $('.line').each(function() {

         ages[ages.length] = $('#'+this.id+'').data("rtt").age;           
         ids[ids.length] = this.id;
    });    
    ages.sort(sortByAge);
    $(ages).each(function(i, v) {             
        for(key in ids)
        {
            if($("#"+ids[key]).data("rtt").age == v)
            {
                 $('#check').append('-> '+v+' - '+ids[key]+'<br />');   
            }
        }

    });

}
function sortByAge(a, b)
{
      return a-b;
}

嗨,我更新了我的答案追加分類股利。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM