Discuss / JavaScript / 交作业!

交作业!

Topic source

秋雨mac

#1 Created at ... [Delete] [Delete and Lock User]
var arr = [];
var list = document.getElementById('test-list');
for (let i = 0; i < list.children.length; i++) {
    arr.push(list.children[i]);
};
arr.sort(function(x, y) {
    if (x.innerText < y.innerText) {
        return -1;
    } else if (x.innerText > y.innerText) {
        return 1;
    } else {
        return 0;
    }
});
for (var x of arr) {
    list.appendChild(x);
}

  • 1

Reply