Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
212 views
in Technique[技术] by (71.8m points)

vue中js操作dom获取属性值刷新后无法获取。

项目开发中需要用js操作dom元素获取元素高度值后进行操作,使用的是原生的方法,在页面第一次加载和刷新页面时会出现无法获取到元素的高度值,在其他页面跳转过来时则可以获得高度值。

<template>
    <img src="../assets/diagram/bottom.png" class="imgBox" ref="bottomImg" />
</template>
<script>
    methods:{
        getHeigth(){
            var bottomHeight = document.getElementsByClassName("imgBox")[0].offsetHeight;
            console.log(bottomHeight);
        }
    },    
    mounted(){
        this.getHeigth();
        window.onresize = () =>{
            this.getHeigth();
        }
    }
<script>

在其他页面跳转过来可以输出高度,页面尺寸变化时也可以正常获取,只有在此页面刷新则无法获取高度。
而且最奇怪的时如果我console.log(document.getElementsByClassName("imgBox"));是有height值且正确的,但是无论我获取这个值的height还是offsetHeight属性都是0。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

你在图片@load后在获取试试 或者在@load事件中加$nexttick


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...