vue中的实现
html
<template>
<el-form>
<input type="text" class='hide' id="loginUserName"/>
<input type="password" class='hide' id="loginPassword"/>
<el-input
ref="text1"
@input="changeUser"
placeholder="账号"
v-model.trim="formData.user"
maxlength="100"
/>
<el-input
ref="password1"
@input="changePwd"
placeholder="密码"
type="password"
v-model.trim="formData.pwd"
maxlength="100"
autocomplete="new-password"
>
</el-input>
</el-form>
</template>
javascript
var flag1 = true
var flag2 = true
export default {
data(){
return {
formData: {
user: '',
pwd: '',
}
}
},
.....
methods: {
changeUser(v) {
if (!v && flag1 ) {
flag1 = false
setTimeout(() => {
this.$refs.text1.blur()
this.$refs.text1.focus()
flag1 = true
}, 20)
}
},
changePwd(v) {
if (!v && flag2 ) {
flag2 = false
setTimeout(() => {
this.$refs.password1.blur()
this.$refs.password1.focus()
flag2 = true
}, 20)
}
},
}
.....
mounted() {
setTimeout(() => {
let vm = this
if (vm.$refs.password1 && vm.$refs.password1.$refs.input) {
vm.$refs.password1.$refs.input.onmousedown = function() {
vm.changeUser(vm.formData.pwd)
}
}
if (vm.$refs.text1 && vm.$refs.text1.$refs.input) {
vm.$refs.text1.$refs.input.onmousedown = function() {
vm.changePayeeNme(vm.formData.user)
}
}
}, 100);
}
}
css
.hide{
width: 0;
border: none;
position: absolute
}