function sortAndReturnTextContent() { const list = document.getElementById("myList"); let items = Array.from(list.children); // 在此补全代码 const numMatcg = /\d+/; list.innerHTML = items .sort((a, b) => +a.id.match(numMatcg) - +b.id.match(numMatcg)) .map((item) => item.outerHTML) .join("\n"); }
通过Array.from()转换数组,
- 采用Sort进行排序,
- 使用正则匹配,提取ID中的数字并使用(+变量名)转换成Number类型,
- 采用Map,提取成员HTMLElement中的outerHTML属性
- 最后Join,将成员拼接成字符串写入list内