1、方法1
获取dom节点可以用ref属性,这个属性就是来获取dom对象的。看代码 这个属性就相当于给这个标签起了一个id样的东西
<template>
<div id="app" v-cloak>
<input type="text" ref="userinfo" value="binge" />
<input type="button" value="按钮" @click="show">
</div>
</template>
<script>
export default {
name: 'app',
data () {
return {
message:'hello world'
}
},
methods: {
// 这个 methods属性中定义了当前Vue实例所有可用的方法
show: function () {
alert(this.$refs.userinfo.value)
}
}
}
</script>
使用ref,给相应的元素加ref=“name” 然后再this.$refs.name获取到该元素
2、方法2
直接给相应的元素加id,然后再document.getElementById(“id”);获取,然后设置相应属性或样式