vue.js怎么获取dom元素?下面本篇文章给大家介绍一下vue.js获取dom元素的方法。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。
使用Vue.js获取dom元素
标签中添加ref属性,相当于id
在Vue中通过 $refs.ref的属性名 获取或者设置dom元素
点击这里
西南交大
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"
}
}
})
更多web前端开发知识,请查阅 HTML中文网 !!