vue2?web多標(biāo)簽輸入框elinput是否當(dāng)前焦點詳解
又來分享一點點工作積累及解決方案
產(chǎn)品中需要用戶輸入一些文字后按下回車鍵生成標(biāo)簽來顯示在頁面上,經(jīng)過嘗試與改造完成如下:


<template>
<div class="tags-view" @click="beginInput">
<el-tag :key="index" v-for="(tag, index) in dynamicTags" closable :disable-transitions="false"
@close="handleClose(index)">
{{ tag }}
</el-tag>
<el-input v-if="inputVisible" class="input-new-tag" style="width: 100%;" v-model="inputValue" ref="saveTagInput"
size="small" @keyup.enter.native="handleInputConfirm" @blur="handleInputConfirm">
</el-input>
<!-- <el-button v-else class="button-new-tag" size="small" @click="showInput">+</el-button> -->
</div>
</template>
<script>
export default {
name: 'inputTag',
props: {
tags: {
type: Array,
default: []
},
},
watch: {
tags: {
deep: true,
immediate: true,
handler(val) {
this.dynamicTags = val || []
}
}
},
data() {
return {
dynamicTags: [],
inputVisible: false,
inputValue: ''
};
},
methods: {
handleClose(index) {
this.dynamicTags.splice(index, 1);
},
showInput() {
this.inputVisible = true;
this.$nextTick(_ => {
this.$refs.saveTagInput.$refs.input.focus();
});
},
beginInput() {
this.showInput();
},
handleInputConfirm() {
let inputValue = this.inputValue;
if (inputValue) {
this.dynamicTags.push(inputValue);
}
const inputElement = this.$refs.saveTagInput.$refs.input; // 獲取input DOM元素
const isFocused = document.activeElement === inputElement; // 判斷是否為當(dāng)前焦點
this.inputVisible = isFocused;
this.inputValue = '';
this.$emit('changed', this.dynamicTags)
}
}
}
</script>
<style lang="scss" scoped>
.tags-view {
display: flex;
flex-direction: row;
justify-content: flex-start;
align-items: center;
flex-wrap: wrap;
min-height: 32px;
padding: 4px 5px;
border: 1px solid #DCDFE6;
border-radius: 4px;
}
.button-new-tag {
margin-left: 10px;
height: 24px;
line-height: 24px;
padding-top: 0;
padding-bottom: 0;
}
.input-new-tag {
height: 24px;
line-height: 24px;
width: 90px;
//margin-left: 10px;
vertical-align: bottom;
}
::v-deep {
.el-tag {
margin-left: 5px;
margin-top: 2px;
margin-bottom: 2px;
}
.el-input__inner {
height: 24px;
line-height: 24px;
border: none;
padding: 0px 5px;
}
}
</style>組件的使用:
import InputTag from '../components/inputTag.vue'
tags用于默認(rèn)值的回調(diào),changed事件用于組件數(shù)據(jù)發(fā)生變化時的回調(diào)通知。
<InputTag class="w-100" :tags="tagsValue" @changed="tagsChanged"></InputTag>
組件本身也比較簡單,沒有啥值得去細(xì)分和品評的技術(shù)點
enter事件和blur事件走了同一個事件,會導(dǎo)致輸入不連續(xù),為解決這個問題,我們只需要判斷當(dāng)前input是不是焦點,如果是,則不隱藏輸入框即可,如下isFoucsed變量的判斷即為是否本身自己是當(dāng)前焦點的input!
handleInputConfirm() {
let inputValue = this.inputValue;
if (inputValue) {
this.dynamicTags.push(inputValue);
}
const inputElement = this.$refs.saveTagInput.$refs.input; // 獲取input DOM元素
const isFocused = document.activeElement === inputElement; // 判斷是否為當(dāng)前焦點
this.inputVisible = isFocused;
this.inputValue = '';
this.$emit('changed', this.dynamicTags)
}總結(jié)
到此這篇關(guān)于vue2 web多標(biāo)簽輸入框elinput是否當(dāng)前焦點的文章就介紹到這了,更多相關(guān)vue2 elinput是否當(dāng)前焦點內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue中的v-model原理,與組件自定義v-model詳解
這篇文章主要介紹了vue中的v-model原理,與組件自定義v-model詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08
Element-ui/Element-plus?Vue報錯問題及解決
這篇文章主要介紹了Element-ui/Element-plus?Vue報錯問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-01-01
vue實現(xiàn)在線預(yù)覽pdf文件和下載(pdf.js)
這篇文章主要為大家詳細(xì)介紹了vue實現(xiàn)在線預(yù)覽pdf文件和下載,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-11-11
vue3實現(xiàn)markdown預(yù)覽和編輯詳解
本文介紹了如何在Vue3項目中集成Vditor Markdown編輯器,包括Vditor的簡介、安裝、預(yù)覽、編輯以及一些高級功能和常見問題解決,Vditor支持多種編輯模式、圖表、公式等,且高度可定制化2025-10-10
Vue組件大全包括(UI組件,開發(fā)框架,服務(wù)端,輔助工具,應(yīng)用實例,Demo示例)
本文為大家分享了網(wǎng)上比較流行的Vue組件,包括UI組件,開發(fā)框架,服務(wù)端,輔助工具,應(yīng)用實例,Demo示例等開源項目,總有一款適合你2018-10-10
VUE數(shù)組根據(jù)索引刪除數(shù)據(jù),頁面同時更新的實現(xiàn)方法
這篇文章主要介紹了VUE數(shù)組根據(jù)索引刪除數(shù)據(jù),頁面同時更新的實現(xiàn)方法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07

