<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title>test</title> <script type="text/javascript" src="http://code.jquery.com/jquery-1.11.1.min.js"></script> </head> <body> <ul class="orderobj"> <li class="preview" a="2">2</li> <li class="preview" a="5">5</li> <li class="preview" a="1">1</li> <li class="preview" a="9">9</li> <li class="preview" a="8">8</li> </ul> <button onclick="test()">测试</button> <script language="JavaScript" type="text/JavaScript"> function test(){ var items = $(".orderobj li").get(); items.sort(function(a,b) { var A = $(a).attr(‘a‘); var B = $(b).attr(‘a‘); if(A < B) return -1; if(A > B) return 1; return 0; }); var ul = $(".orderobj"); $.each(items,function(i,li) { ul.append(li); }); } </script> </body> </html>
时间: 2024-11-26 19:49:45