开发者

jquery reordering of children based on value of title attribute

With jQuery, how can I reorder the list below by the title attribute from largest to smallest (i.e. rateplugin6 would be the first, rateplugin2 would be second, rateplugin8 would be third, rateplugin3 would be fourth, and so on)?

<div id="rateWrapper">
 <div id="rateplugin1" title="2"></div>
 <div id="rateplugin2" title="4"></div>
 <div id="rateplugin3" title="3">&l开发者_运维百科t;/div>
 <div id="rateplugin4" title="1"></div>
 <div id="rateplugin5" title="0"></div>
 <div id="rateplugin6" title="5"></div>
 <div id="rateplugin7" title="1"></div>
 <div id="rateplugin8" title="4"></div>
</div>


What about this blog post?
You will have to modify the sort function (called sortAplha) in the example.

How to get value from that class? You can use substring:

var class = $(this).attr('class');
var classValue = parseInt(class.substring(10)); // should remove 'rateplugin' and convert the rest to int

So the sorting function in your case might look like:

function yourSort(a,b){
  var aClass = a.attr('class');
  var aValue = parseInt(aClass.substring(10));
  var bClass = b.attr('class');
  var bValue = parseInt(bClass.substring(10));

  return aValue < bValue ? 1 : -1;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜