代码:
<template>
<div>
<el-input v-model="input" placeholder="用户名" @keyup.enter.native='a1()'></el-input>
<el-input v-model="input2" placeholder="密码" ref="password"></el-input>
</div>
</template>
<script>
export default {
data () {
return {
input: '',
input2: ''
}
},
methods:{
a1:function(){
this.$refs.password.focus()
}
}
}
</script>
主要要点:
1、在使用element组件中的输入框时点击回车触发事件要加上.native,如:@keyup.enter.native。原因是element-ui自身封装了一层input标签之后,把原来的事件隐藏了。
2、使用ref和$refs获取元素,这个看代码理解就可以了。