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


<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)前焦點(diǎn)
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ā)生變化時(shí)的回調(diào)通知。
<InputTag class="w-100" :tags="tagsValue" @changed="tagsChanged"></InputTag>
組件本身也比較簡(jiǎn)單,沒(méi)有啥值得去細(xì)分和品評(píng)的技術(shù)點(diǎn)
enter事件和blur事件走了同一個(gè)事件,會(huì)導(dǎo)致輸入不連續(xù),為解決這個(gè)問(wèn)題,我們只需要判斷當(dāng)前input是不是焦點(diǎn),如果是,則不隱藏輸入框即可,如下isFoucsed變量的判斷即為是否本身自己是當(dāng)前焦點(diǎn)的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)前焦點(diǎn)
this.inputVisible = isFocused;
this.inputValue = '';
this.$emit('changed', this.dynamicTags)
}總結(jié)
到此這篇關(guān)于vue2 web多標(biāo)簽輸入框elinput是否當(dāng)前焦點(diǎn)的文章就介紹到這了,更多相關(guān)vue2 elinput是否當(dāng)前焦點(diǎn)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue中的v-model原理,與組件自定義v-model詳解
這篇文章主要介紹了vue中的v-model原理,與組件自定義v-model詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08
Element-ui/Element-plus?Vue報(bào)錯(cuò)問(wèn)題及解決
這篇文章主要介紹了Element-ui/Element-plus?Vue報(bào)錯(cuò)問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01
vue實(shí)現(xiàn)在線預(yù)覽pdf文件和下載(pdf.js)
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)在線預(yù)覽pdf文件和下載,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11
Vue實(shí)現(xiàn)手機(jī)掃描二維碼預(yù)覽頁(yè)面效果
這篇文章主要為大家詳細(xì)介紹了Vue實(shí)現(xiàn)手機(jī)掃描二維碼預(yù)覽頁(yè)面效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-03-03
vue3實(shí)現(xiàn)markdown預(yù)覽和編輯詳解
本文介紹了如何在Vue3項(xiàng)目中集成Vditor Markdown編輯器,包括Vditor的簡(jiǎn)介、安裝、預(yù)覽、編輯以及一些高級(jí)功能和常見(jiàn)問(wèn)題解決,Vditor支持多種編輯模式、圖表、公式等,且高度可定制化2025-10-10
Vue組件大全包括(UI組件,開發(fā)框架,服務(wù)端,輔助工具,應(yīng)用實(shí)例,Demo示例)
本文為大家分享了網(wǎng)上比較流行的Vue組件,包括UI組件,開發(fā)框架,服務(wù)端,輔助工具,應(yīng)用實(shí)例,Demo示例等開源項(xiàng)目,總有一款適合你2018-10-10
VUE數(shù)組根據(jù)索引刪除數(shù)據(jù),頁(yè)面同時(shí)更新的實(shí)現(xiàn)方法
這篇文章主要介紹了VUE數(shù)組根據(jù)索引刪除數(shù)據(jù),頁(yè)面同時(shí)更新的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07
Vue2.0如何發(fā)布項(xiàng)目實(shí)戰(zhàn)
本篇文章主要介紹了Vue2.0如何發(fā)布項(xiàng)目實(shí)戰(zhàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-07-07

