关于使用uniapp(uv-view/uview)开发微信小程序,input输入框聚焦全选的问题

通过查阅uniapp官方文档可以知道,需要使用focus、selection-start及selection-end这三个参数才能实现聚焦全选

原文地址:input | uni-app官网

问题

在input组件中的@focus中切换focus状态,并且设置selection-start及selection-end,并没有实现聚焦全选的效果

最后多次实验发现,需要使用v-if切换组件才能实现聚焦全选的效果,代码如下:

<script>
import { ref } from 'vue'

const val = ref('')
const focus = ref(false)
const selectionStart = ref(0)
const selectionEnd = ref(0)

function handleClick() {
  selectionEnd.value = val.value.length
  focus.value = true
}
</script>
<template>
  <view class="w-full h-full">
    <view class="common-style" @click="handleClick" v-if="!focus">
      <uv-input
        readonly
        :focus="focus"
        :selectionStart="0"
        :selectionEnd="selectionEnd"
        v-model="val"
      >
      </uv-input>
    </view>
    <view class="common-style" v-else>
      <uv-input
        :focus="focus"
        :selectionStart="0"
        :selectionEnd="selectionEnd"
        v-model="val"
      >
      </uv-input>
    </view>
  </view>
</template>

注意

需要在input外包裹一层<view></view>,否则在真机调试的时候会出现input叠加的情况,微信开发者工具上显示正常

本人使用的是uv-view组件库开发,此方式也适用于uniapp官方input组件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值