joe*_*joe 5 html sorting jquery
尝试一堆没有可行结果的解决方案.我有代码获取跨度的值并为LI创建一个ID.然后我想根据LI的ID对这些LI的DESCENDING进行排序.救命?谢谢!
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<ul id="dumb">
<li>cello<span>2987</span></li>
<li>zello<span>1723</span></li>
<li>aello<span>3476</span></li>
</ul>
<script type='text/javascript' src='JQUERY INCLUDE'></script>
<script type="text/javascript">
$(document).ready(function() {
$('ul li span').each(function(){
var pubValue = $(this).html();
$(this).parent().attr('id', pubValue);
});
});
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
jAn*_*ndy 11
让我们假设你li nodes有ids.
<ul id="test">
<li id="4112">blub</li>
<li id="1422">blaaah</li>
<li id="6640">hmmmm</li>
<li id="2221">one more</li>
</ul>
Run Code Online (Sandbox Code Playgroud)
然后你可以调用javascripts本机数组.sort()方法,因为jQuery包装集保存在Arrays:
$(function(){
var elems = $('#test').children('li').remove();
elems.sort(function(a,b){
return parseInt(a.id) > parseInt(b.id);
});
$('#test').append(elems);
});
Run Code Online (Sandbox Code Playgroud)
工作示例:http://www.jsfiddle.net/3uYUq/