通过ref来获取DOM节点

 1 <template>
 2   <div>
 3     <div ref="hello">
 4       hello world
 5     </div>
 6     <button @click="handleClick">我是按钮</button>
 7   </div>
 8 </template>
 9 
10 <script>
11 export default {
12   name: 'Home',
13   data () {
14     return {
15       arr: ''
16     }
17   },
18   methods: {
19     handleClick () {
20       this.arr = this.$refs.hello.innerHTML //获取DOM元素节点的值
21       alert(this.arr)
22     }
23   }
24 }
25 </script>

注意:在HTML中时ref,而在methods中是refs,因为在HTML中ref可能有很多,而在methods是通过总的ref来寻找

猜你喜欢

转载自www.cnblogs.com/lanyb009/p/9235809.html