今天有个需求,弹出一个窗口,窗口里只有一个INPUT,要求始终聚焦在窗口中的输入框中,不管用户点哪里,都无法改变焦点,除非关闭这个弹窗。
部份代码如下:
<el-form ref="form">
<el-input v-model="codeInput" ref="codeInput" @blur="focusInput" placeholder="请输入" :autofocus="true" clearable></el-input>
</el-form>
<script>
export default {
data () {
return {
codeInput: ''
}
},
methods: {
focusInput () {
const that = this
setTimeout(() => {
that.$refs.codeInput.focus()
}, 0)
}
}
}
</script>
重点在setTimeout上,一般我们习惯用$nextTick,但很可惜,这在firefox中行不通,只在chrome中能正常锁定焦点。