How can I sort elements by numerical value of data attribute?

前端 未结 5 999
清歌不尽
清歌不尽 2020-11-27 03:07

I have multiple elements with the attribute: data-percentage, is there a way of sorting the elements into ascending order with the lowest value first using eit

5条回答
  •  一个人的身影
    2020-11-27 03:36

    $('.testWrapper').find('.test').sort(function (a, b) {
       return $(a).attr('data-percentage') - $(b).attr('data-percentage');
    })
    .appendTo('.testWrapper');
    

提交回复
热议问题