当前位置: 首页 > news >正文

哈尔滨网站建设科技公司石家庄网络营销网站推广

哈尔滨网站建设科技公司,石家庄网络营销网站推广,惠州seo外包服务,网络设计课程前言 大家好我是没钱的君子下流坯,用自己的话解释自己的知识。很久很更新了,这几个月一直在加班,今天记录一个uniapp关于input中focus()方法自动获取焦点的坑。 案例 为了实现一个手机验证码的页面,验证码是五个输入框&#xf…

前言

大家好我是没钱的君子下流坯,用自己的话解释自己的知识。很久很更新了,这几个月一直在加班,今天记录一个uniapp关于input中focus()方法自动获取焦点的坑。

案例

为了实现一个手机验证码的页面,验证码是五个输入框,输入第一个输入框后焦点自动获取到下一个输入框的焦点如图所示:在这里插入图片描述

实现思路

我的思路是给每个输入框动态绑定一个ref实例去调用实例上的focus()方法,我第一次的代码也是这样写的代码如下所示:

<template><view class="container"><view class="otp-container"><inputv-for="(item, index) in otpLength":key="index"type="tel"maxlength="1"@input="onInput($event, index)"@focus="onFocus(index)":ref="`input-${index}`"class="otp-input"/></view><button @click="submitOTP">提交</button></view>
</template><script>
export default {data() {return {otpLength: 5,         // 验证码的长度otpValues: ['', '', '', '', ''],  // 存储每个输入框的值activeIndex: null     // 当前聚焦的输入框索引};},methods: {onInput(event, index) {const value = event.target.value;this.$set(this.otpValues, index, value);  // 更新对应输入框的值if (value && index < this.otpLength - 1) {this.$nextTick(() => {this.focusNextInput(index + 1);  // 自动跳转到下一个输入框});}},onFocus(index) {this.activeIndex = index;  // 记录当前聚焦的输入框索引},focusNextInput(index) {console.log(`Trying to focus input-${index}`);console.log('this.$refs:', this.$refs);const ref = this.$refs[`input-${index}`];if (ref && Array.isArray(ref) && ref.length > 0) {ref[0].focus();  // 确保引用存在再调用 focus 方法} else {console.error(`Reference for input-${index} not found or invalid`, ref);}},submitOTP() {const otp = this.otpValues.join('');  // 将所有输入框的值拼接成一个字符串console.log('提交的验证码:', otp);  // 打印验证码// 这里可以添加提交验证码的逻辑}}
};
</script>

尝试方法1加判断排除DOM渲染

但是一直报错说 "TypeError: this.$refs["input-".concat(...)][0].focus is not a function" 我心想这怎么可能不是个函数难道未定义,我就抓紧打印ref发现是有这个实例的,当时就没多想又对代码进行了检查然后加了判断,考虑可能是没获取到实例或者dom没渲染出来,因为前面的input是通过v-for循环的所以进行了更全面的判断代码如下所示

    focusNextInput(index) {console.log(`Trying to focus input-${index}`);const ref = this.$refs[`input-${index}`];this.$nextTick(() => {// 检查 ref 是否存在if (Array.isArray(ref) && ref.length > 0 && ref[0].focus) {ref[0].focus();  // 如果是数组形式,使用 ref[0] 并调用 focus} else if (ref && ref.focus) {ref.focus();  // 如果直接是 DOM 元素也调用 focus} else {console.error(`Reference for input-${index} not found or invalid`, ref);}});},

尝试方法2查看ref实例用ref上的方法

发现报错还是"TypeError: this.$refs["input-".concat(...)][0].focus is not a function"我就懵逼了。发现不是代码的问题,问题肯定是出现在了ref实例上,我就仔细的看ref实例里面的所有方法如下图所示
在这里插入图片描述
发现里没有focus()我想这一次可算找到根本了一次解决直接换成_focus()方法,发现是不报错了但是效果没有,我又换成_onFocus()方法发现也是不报错但是效果没有。到此时为止没有任何思路。

尝试方法3通过uniapp自带获取dom节点

中午吃了饭以后,我想为什么在uniapp中ref实例里面的方法不能用,那我通过uniapp自带的获取dom节点然后通过dom再去控制焦点,说干就干代码如下

    focusNextInput(index) {console.log(`Trying to focus input-${index}`);const ref = this.$refs[`input-${index}`];if (ref && ref.length > 0) {// 使用微信小程序的API来设置焦点console.log(reg,'sssssssssssss')this.$nextTick(() => {uni.createSelectorQuery().in(this).select(`#input-${index}`).fields({ node: true }).exec((res) => {res[0].node._onFocus();  // 使用 focus 方法设置焦点});});} else {console.error(`Reference for input-${index} not found or invalid`, ref);}},

我把这段代码修改后发现使用focus()还不行_focus()方法_onFocus()方法也不行

解决办法

上面我是所能用的方法都用了,这时候我想着看看官网吧,发现uniapp有自带的获取焦点的方法,只有用官网的方法才可以也就是<input :focus="focusState" @blur="dataExamine()"></input>这种,我心想那直接控制:focus实现代码如下所示:

<template><view class="container"><view class="otp-container"><inputv-for="(item, index) in otpLength":key="index"type="tel"v-model="otpValues[index]":focus="activeIndex === index"maxlength="1"@input="onInput($event, index)"@focus="onFocus(index)":ref="`input-${index}`":id="`input-${index}`"class="otp-input"/></view><button @click="submitOTP">提交</button></view>
</template><script>
export default {data() {return {otpLength: 5,          // 验证码的长度otpValues: ['', '', '', '', ''], // 存储每个输入框的值activeIndex: null      // 当前聚焦的输入框索引};},mounted() {// 页面加载完成时绑定 refsthis.$nextTick(() => {console.log('Initial refs:', this.$refs);});},methods: {onInput(event, index) {const value = event.target.value;console.log(event, index, value,'1111111111111111111111111');this.$set(this.otpValues, index, value);  // 更新对应输入框的值if (value && index < this.otpLength - 1) {this.$nextTick(() => {this.focusNextInput(index + 1);  // 自动跳转到下一个输入框});}},onFocus(index) {this.activeIndex = index;  // 记录当前聚焦的输入框索引console.log(this.activeIndex);},focusNextInput(index) {console.log(`Trying to focus input-${index}`);this.activeIndex = index;  // 设置指定输入框为焦点},submitOTP() {const otp = this.otpValues.join('');  // 将所有输入框的值拼接成一个字符串console.log('提交的验证码:', otp);  // 打印验证码// 这里可以添加提交验证码的逻辑}}
};
</script>

到此这个效果算是完成了,uniapp中有很多细节和vue上有偏差,还是需要熟读文档。还是那句话代码代码千千万,适合自己最重要。

http://www.mmbaike.com/news/50786.html

相关文章:

  • 南昌网站建设制作国内做网站比较好的公司
  • 网站升级页面连接设置搜索软件使用排名
  • wordpress 首页判断青岛seo青岛黑八网络最强
  • 政府网站改版建设汇报网站排名软件推荐
  • 没电脑可以建网站吗个人博客网页制作
  • 中山做网站优化关键字挖掘爱站网
  • 长沙网站建设找哪家爱站权重查询
  • 如何快速提升网站pr夸克搜索入口
  • 有什么可以在线做奥数题的网站广告投放网站
  • 支付宝 外贸网站百度网盘搜索引擎盘多多
  • 佛山电子商务网站设计快排seo软件
  • 上海网站建设定制seo在线优化工具
  • 做网站时用插件需要注明吗网络推广的主要工作内容
  • 时时彩网站代理怎么做?口碑营销公司
  • 别人给我们做的网站如何关闭广州网络优化最早的公司
  • 国内免费商用图片的网站怎么建立自己的网站
  • 专业做网站的公司有没有服务器郑州粒米seo外包
  • 沈阳专业网站制作设计公司企业网站建设方案
  • 网站制作需要平台seo优化网站推广专员招聘
  • 网站建站方案seo网站建设是什么意思
  • 互联网+大学生创新创业项目百度seo推广软件
  • 做网站需要什么开发语言品牌宣传推广策划方案
  • 建一个做笔记的网站给你一个网站怎么优化
  • 珠海网站建设陈玉铭免费推广自己的网站
  • 小程序网站做多大尺寸江苏seo平台
  • 重庆展示型网站制作武汉网站开发公司
  • 个人可以做购物网站吗怎么在百度上推广
  • 城市旅游网站开发cpa广告联盟
  • 建筑设计单位青岛seo代理计费
  • 金融投资公司网站建设论文推广方案设计