實現僞數組轉成真數組
實現方法:Array.prototype.slice.call(僞數組).forEach(()=>{})
示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<title>Document</title>
<style>
ul>li{
list-style: none;
background-color: pink;
margin:10px 0;
}
</style>
</head>
<body>
<div id="app">
<ul ref="ulParent">
<li style="height: 20px;">li-1</li>
<li style="height: 50px;">li-2</li>
<li style="height: 30px;">li-3</li>
<li style="height: 70px;">li-4</li>
<li style="height: 40px;">li-5</li>
</ul>
<button @click="getLiHeight">獲取所以li高度</button>
<div>
<p>所有li高度</p>
{{liHeight}}
</div>
</div>
<script>
const app = new Vue({
el: '#app',
data: {
liHeight:[]
},
methods:{
getLiHeight(){
let lis = this.$refs.ulParent.getElementsByTagName('li');
let liArr = Array.prototype.slice.call(lis);
let liHeight = [];
liArr.forEach(li => {
liHeight.push(li.clientHeight);
});
this.liHeight = liHeight;
}
}
})
</script>
</body>
</html>
示例結果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/a5d13a5738df3c90e7c74608ded78407.png)