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

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

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

又來分享一點點工作積累及解決方案

產(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)文章

最新評論

正安县| 卓资县| 七台河市| 江油市| 濉溪县| 连平县| 和林格尔县| 永和县| 万荣县| 松江区| 五原县| 香河县| 芷江| 宣武区| 嘉义县| 和田县| 阳原县| 湘西| 同心县| 综艺| 罗平县| 荔浦县| 小金县| 庆云县| 巴彦淖尔市| 铜陵市| 宜川县| 汝阳县| 蒙城县| 秦皇岛市| 陕西省| 滁州市| 高雄县| 黄冈市| 福州市| 克拉玛依市| 韶关市| 河曲县| 宁陵县| 大庆市| 古丈县|