首页 > 编程语言 > 详细

javascript中li标签的排序和数组sort的用法

时间:2019-01-08 11:55:14      阅读:184      评论:0      收藏:0      [点我收藏+]

 

转:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <script type="text/javascript">
        window.onload = function () {
            var oBtn = document.getElementById("btnChange");
            var oUl1 = document.getElementById("ul1");
            oBtn.onclick = function () {
 
                var aLi = oUl1.getElementsByTagName("li");
                var arr = [];
                for (var i = 0; i < aLi.length; i++) {
                    arr[i] = aLi[i];
                }
                arr.sort(function (li1, li2) {
                    var n1 = parseInt(li1.innerText);
                    var n2 = parseInt(li2.innerText);
                    return n1 - n2;
                });
                for (var i = 0; i < aLi.length; i++) {
                    oUl1.appendChild(arr[i]);
                }
            }
        }
    </script>
</head>
<body>
    <input type="button" name="" value="移动" id="btnChange" />
    <ul id="ul1">
        <li>5</li>
        <li>2</li>
        <li>9</li>
        <li>4</li>
        <li>7</li>
    </ul>
</body>
</html>

javascript中li标签的排序和数组sort的用法

原文:https://www.cnblogs.com/libin6505/p/10237676.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!