vue2?web多標(biāo)簽輸入框elinput是否當(dāng)前焦點(diǎn)詳解
又來分享一點(diǎn)點(diǎn)工作積累及解決方案
產(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)前焦點(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>
組件本身也比較簡單,沒有啥值得去細(xì)分和品評(píng)的技術(shù)點(diǎn)
enter事件和blur事件走了同一個(gè)事件,會(huì)導(dǎo)致輸入不連續(xù),為解決這個(gè)問題,我們只需要判斷當(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 data的數(shù)據(jù)響應(yīng)式到底是如何實(shí)現(xiàn)的
這篇文章主要介紹了Vue data的數(shù)據(jù)響應(yīng)式到底是如何實(shí)現(xiàn)的,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02
Vue下載Excel后報(bào)錯(cuò),或打不開的解決
這篇文章主要介紹了Vue下載Excel后報(bào)錯(cuò),或打不開的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02
使用vue-cli(vue腳手架)快速搭建項(xiàng)目的方法
本篇文章主要介紹了使用vue-cli(vue腳手架)快速搭建項(xiàng)目的方法,vue-cli 是一個(gè)官方發(fā)布 vue.js 項(xiàng)目腳手架,使用 vue-cli 可以快速創(chuàng)建 vue 項(xiàng)目,感興趣的小伙伴們可以參考一下2018-05-05
前端本地存儲(chǔ)方案localForage在vue3中使用方法
localForage是前端本地存儲(chǔ)的庫,支持多種存儲(chǔ)后端,并提供了一致的API來存儲(chǔ)和檢索數(shù)據(jù),這篇文章主要給大家介紹了關(guān)于前端本地存儲(chǔ)方案localForage在vue3中使用的相關(guān)資料,需要的朋友可以參考下2024-09-09
vue移動(dòng)端實(shí)現(xiàn)紅包雨效果
這篇文章主要為大家詳細(xì)介紹了vue移動(dòng)端實(shí)現(xiàn)紅包雨效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07
vue中使用webuploader做斷點(diǎn)續(xù)傳實(shí)現(xiàn)文件上傳功能
之前做的一個(gè)項(xiàng)目中,由于經(jīng)常上傳幾百兆的壓縮包,導(dǎo)致經(jīng)常上傳失敗,所以就找了webuploader插件做了斷點(diǎn)續(xù)傳,斷點(diǎn)續(xù)傳除了需要前端分片,也需要后臺(tái)去支持,所以做的時(shí)候做好對(duì)接協(xié)調(diào),所以本文就給大家詳細(xì)的介紹一下vue中如何使用webuploader做斷點(diǎn)續(xù)傳2023-07-07
vue父組件與子組件之間的數(shù)據(jù)交互方式詳解
最近一直在做一個(gè)vue移動(dòng)端商城的實(shí)戰(zhàn),期間遇到一個(gè)小小的問題,值得一說,下面這篇文章主要給大家介紹了關(guān)于vue父組件與子組件之間數(shù)據(jù)交互的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-11-11

