<!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>
Logo

前往低代码交流专区

更多推荐