<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>如何将伪数组 NodeList集合 转换为真正的数组</title>
</head>
<body>
<ul id="list">
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
</body>
</html>
<script type="text/javascript">
//先获取NodeList集合,也就是这里的li集合
let NodeList=document.getElementById('list').children;
//方法一:
//思路:先定义一个空数组。然后让这个NodeList集合的每个元素添加到空数组中
let arr=[];
for(let i=0;i<NodeList.length;i++){
arr.push(NodeList[i]);
}
console.log(arr);
//方法二:(ES6)直接用Array.from()
let arr2=Array.from(NodeList);
console.log(arr2);
</script>
谈谈伪数组NodeList
最新推荐文章于 2024-05-22 23:41:41 发布