最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue2?web多標(biāo)簽輸入框elinput是否當(dāng)前焦點(diǎn)詳解

 更新時(shí)間:2025年01月21日 09:29:05   作者:zhaocarbon  
這篇文章主要介紹了vue2?web多標(biāo)簽輸入框elinput是否當(dāng)前焦點(diǎn)的相關(guān)資料,講解了如何在產(chǎn)品中實(shí)現(xiàn)用戶輸入文字后按下回車鍵生成標(biāo)簽并顯示在頁(yè)面上的功能,通過(guò)組件的使用和改造,解決了輸入不連續(xù)的問(wè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)文章

最新評(píng)論

个旧市| 二连浩特市| 宁德市| 金川县| 秦皇岛市| 苏州市| 兖州市| 二连浩特市| 新野县| 江西省| 嘉祥县| 古田县| 长泰县| 商河县| 开化县| 肥东县| 图们市| 漳州市| 云霄县| 灯塔市| 双江| 鄄城县| 祥云县| 南部县| 永年县| 当雄县| 荆州市| 嘉鱼县| 湘潭市| 怀宁县| 察哈| 古田县| 遵化市| 安图县| 余庆县| 专栏| 阜宁县| 区。| 河东区| 色达县| 兴化市|