在vue中操作dom,要在mounted中使用,因为只有在执行mounted的时候,vue已经渲染了dom节点,这个时候是可以获取dom节点的,vue中尽量不去操作dom元素,选用ref操作属性获取

首先设置ref=“xxx” 然后等所有的dom加载完毕之后,使用this.$ref.btn来进行操作

<button ref="btn">获取ref</button>
this.$refs.btn.style.backgroundColor="#ff0000"