js 将获取到的dom伪数组转为真数组

用js获取到的dom伪数组无法执行for循环便利数组,需要将伪数组转为真数组,方法如下:

	var liList = document.querySelectorAll('li')
	// 这里用forEach遍历 需要把伪数组转为真数组
	// ES5语法 转为真数组
	// let newList = Array.prototype.slice.call(liList)
	// ES6语法 转为真数组
	let newList = Array.from(liList)
	newList.forEach((item,index,array)=>{
		console.log(item,index);
	})
发布了100 篇原创文章 · 获赞 8 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/Misnice/article/details/104579731
今日推荐