首页 > 其他分享 >跳转页面input输入框自动聚焦,兼容安卓和ios

跳转页面input输入框自动聚焦,兼容安卓和ios

时间:2023-05-11 13:34:37浏览次数:34  
标签:dom ios focus 输入框 跳转 autofocusFn input 页面

需求是这样:点击某个页面或某个页面的模块,跳转到带搜索的子页面,并且自动弹出手机软键盘。

长话短说,代码:

// 封装一个自动聚焦的函数,兼容ios和安卓
const autofocusFn = () => {
  const doc = document
  const input = doc.querySelector('#hide-focus-input')
  let dom = null
  // 1.检查文档有没有一个id为hide-focus-input的input标签
  // 2.如果有,dom等于这个input标签
  // 3.如果没有,则创建一个id为hide-focus-input的input标签,并添加样式(第一次执行时走这步)
  // 4.调通input的原生focus方法来聚焦
  if (input) {
    dom = input
  } else {
    dom = doc.createElement('input')
    dom.setAttribute('id', 'hide-focus-input')
    dom.style = 'position: fixed; z-index: -9999; width: 0; height: 0'
    doc.body.appendChild(dom)
  }
  dom.focus()
}
//如果是封装在一个js文件,则暴露出去,在用的地方用import引入
export { autofocusFn }

 

函数已经封装好了,接下来就是调用:

1.先引入autofocusFn 函数

2.跳转前调用(比如在VUE里面使用)

<template>
  <div @click="searchClick">点击搜索</div>
</template>
<script>
import { autofocusFn } from '你放函数的文件路径'
export default {
  methods: {
    searchClick() {
      autofocusFn()
      this.$router.push('真正带搜索input的页面')
    },
  },
}
</script>

 

最后一步,在需要弹出键盘的页面手动调用input的聚焦方法

比如普通input:

<template>
  <div>
    <input v-model="searchVal" autofocus ref="searchRef" />
  </div>
</template>

<script>
export default {
  data() {
    return {
      searchVal: '',
    }
  },
  mounted() {
    this.$refs.searchRef.focus()
  },
}
</script>

<style></style>

 

比如在vant:
<template>
  <div>
    <van-search v-model="searchVal" autofocus ref="searchRef" />
  </div>
</template>

<script>
export default {
  data() {
    return {
      searchVal: '',
    }
  },
  mounted() {
    this.$refs.searchRef.focus()
  },
}
</script>

<style></style>

 

 

标签:dom,ios,focus,输入框,跳转,autofocusFn,input,页面
From: https://www.cnblogs.com/wiliam/p/17390780.html

相关文章

  • react echarts tooltip 区域新加一个输入框,可以编辑保存数据
     //demo页面//需求:产品要求在折线图的tooltip上新加一个输入框,可以编辑这个输入框保存备注信息,需要两种交互方式:1.鼠标滑过展示备注信息。2.鼠标点击某一个日期时,鼠标可以滑到tooltip上做保存/编辑操作。//思路:1.保留初始鼠标滑过echarts图效果。//2.主要难点是点击时to......
  • uniapp跳转到一个原生的页面
    一、创建原生页面并配置1.创建原生页面Demo2.配置原生页面继承Activity二、在继承UniModule的类中写对应的页面跳转逻辑三、在uniapp中配置跳转的按钮四、将打包好的本地资源替换到项目中,打包运行1.本地资源打包2.替换掉AndroidStudio项目中的文件3.编译运行,点击进入小镜......
  • uniapp自定义开发一个文本输入框
    开发component中的一个input标签一、在原来的模块上面创建一个新的类TestComponent1.新建TestComponent2.配置json文件二、uniapp准备工作1.在uniapp中写一下刚刚创建的输入框2.打包导出资源3.资源替换复制刚刚生成的本地资源文件夹到AndroidStudio项目中......
  • nginx 访问域名跳转至域名后接目录
    要实现https://xxx.com/自动跳转至https://xxx.com/new,可以在Nginx的配置文件中添加以下重定向规则:server{listen80;listen[::]:80;server_namexxx.com;return301https://$server_name$request_uri;}server{listen443ssl;listen......
  • ios 的UI设计
    颜色https://codershigh.github.io/guidelines/ios/human-interface-guidelines/visual-design/color/index.htmldark模式适配https://www.kodeco.com/10718147-supporting-dark-mode-adapting-your-app-to-support-dark-mode#toc-anchor-009......
  • 直播网站源码,安卓防止输入框自动弹出
    直播网站源码,安卓防止输入框自动弹出可以在edittext的父布局结构中(例如LinearLayout,RelativeLayout等)添加  android:focusable="true" android:focusableInTouchMode="true"​以上就是直播网站源码,安卓防止输入框自动弹出,更多内容欢迎关注之后的文章 ......
  • axios
    json-server​JSONServer是一个快速、零配置的基于Node.js的FakeRESTAPI服务器,用于快速原型开发和前端开发中的数据模拟。JSONServer的核心功能是提供基于RESTfulAPI的数据访问接口,它支持常用的HTTP方法,如GET、POST、PUT、DELETE等,可以方便地进行数据的增......
  • ios如何集成百度地图---还是自己动手做一下--网上好多博客说的都不甚靠谱。
    1:申请key,在appdelegate中加入相应的代码。比如。appdelegate.h中:#import<UIKit/UIKit.h>#import<FMDB.h>#import<BaiduMapAPI_Base/BMKBaseComponent.h>//引入base相关所有的头文件#import<BaiduMapAPI_Map/BMKMapComponent.h>//引入地图功能所有的头文件......
  • vue2+vant2+rem+axios+钉钉自动登录 h5模板
    下载代码请轻轻的点一下这里~Node版本要求VueCLI需要Node.js8.9或更高版本(推荐8.11.0+)。你可以使用nvm或nvm-windows在同一台电脑中管理多个Node版本。本示例Node.js14.17.0启动项目npminstallnpmrundev打包项目打测试包会带vconsole控制台对......
  • Vue路由跳转时的动画效果
    1.写一个layout组件,降<router-view/>包裹在transition标签里,实现路由跳转时的动画 2.在router/index.js里面引入该组件,并放在component:layout这里,功能完成 3.transition是vue的封装组件,具体可参考官网 https://cn.vuejs.org/guide/built-ins/transition.html#css-based-......