ref获取dom(Vue06)

it2026-01-24  5

每一个组件(实例)下,都有一个名为“$refs”的属性。打印这个属性,会显示一个对象,里面有添加量ref属性的dom元素。

<div ref="odiv"> <span ref="ospan"> this.$refs { odiv:div, ospan:span } 获取 this.$refs['odiv'] this.$refs.ospan

例:按按钮,字体变蓝

<div id="app"> <div class="box" ref="a">I'm div</div> <!-- 想要获取此元素 --> <button @click="toblue">字体变蓝</button> </div> const vm = new Vue({ el: '#app', methods: { toblue () { this.$refs.a.style.color="blue"; } } })

最新回复(0)