正常情况下,可以使用 el-select 自带的方法 blur 事件来使select失去焦点
示例:
<el-select v-model="value" ref="selectRef">
<el-option
v-for="item in options"
:key="item.value"
:label="item.label"
:value="item.value"
/>
</el-select>
<script setup lang="ts">
import { ref } from 'vue';
const selectRef = ref()
//清除select聚焦
const clearSelectFoucs = ()=>{
selectRef.value.blur()
}
</script>
某些特殊情况下,直接调用 blur 事件会不生效,可以采用以下方法:
<script setup lang="ts">
import { ref, nextTick } from 'vue';
const selectRef = ref()
//方法一
const clearSelectFoucs = ()=>{
nextTick(()=>{
selectRef.value.blur()
})
}
//方法二
const clearSelectFoucs = ()=>{
//先让选择框聚焦 再间隔执行失焦 blur 事件
selectRef.value.focus()
setTimeout(() => {
selectRef.value.blur();
}, 200);
}
</script>