vue 实用小技巧

 

 1  实现一个input动态的让后缀跟随

 
                            <div class="label-text-alt text-gray-400">联系方式</div>
                            <div class="flex flex-nowrap  items-center mb-[2px]   h-[24px] w-[180px] pr-[6px] py-[2px] rounded-[2px] relative "
                                 :class="{ 'border border-[#D4D4D7] bg-[#eee] justify-between': isEditTop }">
                                <div :contenteditable="isEditTop" @keyup="v =>person.phone = v.target.innerHTML"
                                     class="h-[24px]  justify-center  focus:outline-none"
                                     :class="{ 'w-[134px]': isEditTop }">{{ person.phone }}
                                </div>
                                <label class="tag-text ml-[8px]">Mobile</label>
                            </div>
                            <div class="flex flex-row items-center mb-[2px]  h-[24px] w-[180px] pr-[6px] py-[2px] rounded-[2px] relative"
                                 :class="{ 'border border-[#D4D4D7] bg-[#eee] justify-between': isEditTop }">
                                <div :contenteditable="isEditTop" @keyup="v =>person.email = v.target.innerHTML"
                                     class="h-[24px] justify-center focus:outline-none"
                                     :class="{ 'w-[139px]': isEditTop }">{{ person.email }}
                                </div>
                                <label class="tag-text ml-[8px]">Email</label>
                            </div>
                        

:contenteditable 属性

2 自动换行聚焦

const inputs = ref([]);
watch(() => currentIndex.value, () => {
    setTimeout(() => {
        inputs.value[currentIndex.value].focus();
    }, 100);
})
//每在我的input里面按enter以后的话要聚焦到下一个input

<input v-model="data[index - 1 ]"
                                   @focus="setCurrentIndex(index);console.log(currentIndex);console.log(data.length);"
                                   :class="currentIndex===index?'text-blue-400 scale-100':'opacity-25 scale-[90%]'"
                                   class="transition-all w-[71px] h-[25px] border no-rounded text-center  ml-[40px]"
                                   type="text"
                                   :ref="el => inputs[index] = el"
                                   @keydown.enter="scroll.scrollTop = currentIndex * 65;
                                   currentIndex===data.length?currentIndex++&&data.push(id++)&&refsInput.push('input'+data.length)&&console.log(currentIndex)
                                   :currentIndex++;
                                 ">

Vue 在模板中提供了 $refs 来获取组件或者 DOM 元素的引用。这种方式更加符合 Vue 的响应式特性。尝试将 input 元素使用 $refs 获取引用来尝试,这样更加符合 Vue 的方式。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值