方法一、绑定ref
方法二、通过自定义事件中的事件对象 $event,找到input
方法三、使用自定义指令
方法四、使用原生input
方法一、绑定ref——参考yiyueqinghui
<el-input v-model="form.name" ref="name"></el-input>
this.$refs.name.focus();
方法二、通过事件中的事件对象 $event,找到input——参考Z.R.J
<el-input v-model="form.name" ref="name" @key.enter.native="inputFocus($event)"></el-input>
inputFocus(e){
e.target.focus();
e.target.blur(); //让输入框失去焦点
}
方法三、使用自定义指令——官网
<el-input v-model="form.name" ref="name" v-focus></el-input>
directives: {
focus: {
inserted: function (el) {
console.log(el);
//因为el-input这是个组件,input外面被一层 div 包裹着
//el打印出来是外面这个 div,需要找到内层的input
el.children[1].focus();
}
}
}
方法四、使用原生——参考 萝卜爱吃青菜
<input type="text" id="userName" name="username" autofocus="autofocus"/>
this.$nextTick(()=>{
var userName = document.getElementById("userName");
userName.focus();
})