jquery 如何对元素数组进行属性排序
发布网友
发布时间:2022-05-15 03:55
我来回答
共1个回答
热心网友
时间:2022-05-15 05:25
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script class="jquery library" src="/js/sandbox/jquery/jquery-1.8.2.min.js" type="text/javascript"></script>
<title>RunJS 演示代码</title>
<script>
$(function() {
var ic = $(".item_content");
var divs = ic.find("div");
var arr = divs.get();
arr.sort(function(a, b) {
var ai = parseFloat($(a).attr("index"), 10);
var bi = parseFloat($(b).attr("index"), 10);
if (ai > bi) {
return 1;
} else if (ai < bi) {
return -1;
} else {
return 0;
}
});
ic.append(arr);
});
</script>
</head>
<body>
<div class='item_content'>
<div index="131">1</div>
<div index="01">2</div>
<div index="12">3</div>
<div index="13">4</div>
<div index="31">5</div>
<div index="31">6</div>
<div index="17">7</div>
</div>
</body>
</html>