本文环境:windows7、vue2.9.6,该方法适用于所有品牌的电脑。

vue.js找dom元素的方法:

标签中添加ref属性,相当于id

在Vue中通过 $refs.ref的属性名 获取或者设置dom元素

<div id="demo">
        <button @click="fn">点击这里</button>
    <div ref="box" style="width:200px;height:300px">西南交大</div>
    </div>
    <script src="./node_modules/vue/dist/vue.js"></script>
    <script>
        new Vue({
            el: "#demo",
            data: {
               
            },
            methods: {
                fn:function(){
                    console.log(this.$refs.box);
                    console.log(this.$refs.box.innerText);
                    console.log(this.$refs.box.style.width);
                    this.$refs.box.style.backgroundColor="red"
                }
            }
        })
    </script>

以上就是vue.js怎么找dom元素的详细内容,更多请关注其它相关文章!