Sort a set of li tags alphanumerically

前端 未结 3 1268
轮回少年
轮回少年 2021-01-21 05:53

I\'ve been playing around trying to get a function that will sort a selection of li tags by their content but currently to no avail (at least no speed/accuracy);



        
3条回答
  •  野的像风
    2021-01-21 06:40

    var sortList = function () {
        var ul = document.getElementsByTagName("ul"),
        ol = document.getElementsByTagName("ol"),
        sort = function (x) {
            var a, li;
            for (a = 0; a < x.length; a += 1) {
                li = x[a].getElementsByTagName("li");
                li = li.sort();
                x[a].innerHTML = li.join("");
            }
        };
        sort(ul);
        sort(ol);
    };
    

提交回复
热议问题