var ol = document.getElementById('test-list');
Array.from(ol.children)
.sort((x, y) => x.innerText > y.innerText ? 1 : -1)
.forEach(x => ol.appendChild(x));
删除最后的 .forEach(x => ol.appendChild(x));
也是可以的
不行,如果删除forEach,只能得到排序后的节点数组,无法将排序结果应用于dom节点
Sign in to make a reply
Mason
var ol = document.getElementById('test-list');
Array.from(ol.children)
.sort((x, y) => x.innerText > y.innerText ? 1 : -1)
.forEach(x => ol.appendChild(x));