vue里获取ul下全部li元素组成伪数组,转成数组forEach遍历获取每个li的高度
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title></title></head><body><div id="app"><p v-for="count in 10"...
·
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<div id="app">
<p v-for="count in 10">第{{count}}次, 总计 10次</p>
<ul ref="ulParent">
<li style="height: 60px;">1</li>
<li style="height: 30px;">2</li>
<li style="height: 50px;">3</li>
<li style="height: 160px;">4</li>
<li style="height: 10px;">5</li>
</ul>
<button @click="getAllLiHeight()">获取高度数组</button>
<button @click="dealSome()">验证some</button>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
new Vue({
el:'#app',
data: {
dataArr: ['哼哼', 'itlike11.com', '哈哈哈哈']
},
methods: {
getAllLiHeight(){
let liHeightArr = [];
// 1. 获取dom元素
let allLis = this.$refs.ulParent.getElementsByTagName('li');
// console.log(allLis);
// console.log(Array.prototype.slice.call(allLis));
//把伪数组转成数组
Array.prototype.slice.call(allLis).forEach(li => {
//获取每个li的高度,并追加到数组中
liHeightArr.push(li.clientHeight);
});
console.log(liHeightArr);
},
//some方法的使用
dealSome(){
let result = this.dataArr.some((str)=>{
//如果数组里有 'itlike.com'则返回true,没有就返回false
return str === 'itlike.com';
});
console.log(result);
}
}
});
</script>
</body>
</html>
更多推荐
已为社区贡献3条内容
所有评论(0)