Discuss / JavaScript / 排序然后添加

排序然后添加

Topic source

玹帝

#1 Created at ... [Delete] [Delete and Lock User]
ol = document.getElementById('test-list');
lis = Array.from(ol.children);
lis.sort(function (x, y) {
    return x.innerText<y.innerText? -1:1;
});
for (let i = 0; i < lis.length; i++) {
    ol.appendChild(lis[i])
};

HTMLCollection属于类数组,无法直接排序


  • 1

Reply