HTML5 Data Attribute Sort

爱⌒轻易说出口 提交于 2019-11-30 04:56:58

问题


I'd like to sort DOM elements that has data attributes defined for rating and date. What is the best way to implement sorting on the front-end with jQuery?

Sample code can be seen on: http://jsfiddle.net/gercheq/zhqXd/

Here is the desired functionality implemented with tables: http://tablesorter.com/docs/

Thanks,


回答1:


There is a cool jQuery plugin that sorts DOM elements by attribute. You can find it here: http://tinysort.sjeiti.com/

Example implementation: http://jsfiddle.net/statico/JNFFj/7/




回答2:


Here is the basic idea...

var sortedSet = $('#sort li').toArray().sort(function(a, b) {
   return $(a).data('rating') - $(b).data('rating');
});

You select the elements, convert them to a proper array, and then sort (the comparison function I used is an example, change it to suit your requirements).

jsFiddle with lowest button.



来源:https://stackoverflow.com/questions/5560493/html5-data-attribute-sort

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!