Discuss / JavaScript / 参考大牛们的思路,两种方法比较简单

参考大牛们的思路,两种方法比较简单

Topic source

第一种直接对children排序再导入

list = document.getElementById('test-list');
sort = Array.from(list.children).sort((a, b) => a.innerText > b.innerText ? 1:-1);
for (let e of sort) list.appendChild(e);

第二种先提取文本,对文本排序再逐一写入

list = Array.from(document.getElementById('test-list').children);
sort = list.map(element => element.innerText).sort();
list.map((element, index) => element.innerText = sort[index])

江K60

#2 Created at ... [Delete] [Delete and Lock User]

666你才是大牛

紫陌

#3 Created at ... [Delete] [Delete and Lock User]

第二种最后一行, 把map应该修改为forrach,不需要生成新的arr了

list.foreach((element, index) => element.innerText = sort[index])

  • 1

Reply