当前位置:网站首页>自定义指令,获取焦点
自定义指令,获取焦点
2022-08-01 20:01:00 【kilito_01】
// 对Vue的全局指令, 进行封装
// 封装中间件函数插件
const directiveObj = {
install (Vue) {
Vue.directive('focus', {
// el代表指令所在标签
// 指令所在标签, 被插入到真实DOM时才触发, 如果标签用display:none隐藏再出现, 不会在触发inserted的
inserted (el) {
// 指令所在van-search组件
// 组件根标签是div, input在内部
// 以上都是原生标签对象
// 搜索页面 el是div
// 文章评论 el是textarea
// 以后el还可能是input呢
// 知识点: 原生DOM.nodeName 拿到标签名字 (注意: 大写的字符串)
if (el.nodeName === 'TEXTAREA' || el.nodeName === 'INPUT') {
el.focus()
} else {
// el本身不是输入框, 尝试往里获取一下
setTimeout(() => {
const theInput = el.querySelector('input')
const theTextArea = el.querySelector('textarea')
// 判断: 不一定能获取得到, 需要加判断, 有值了, 再执行.focus()才不报错
if (theInput) theInput.focus()
if (theTextArea) theTextArea.focus()
})
}
},
update (el) {
// 指令所在标签, 被更新时触发
if (el.nodeName === 'TEXTAREA' || el.nodeName === 'INPUT') {
el.focus()
} else {
// el本身不是输入框, 尝试往里获取一下
setTimeout(() => {
const theInput = el.querySelector('input')
const theTextArea = el.querySelector('textarea')
// 判断: 不一定能获取得到, 需要加判断, 有值了, 再执行.focus()才不报错
if (theInput) theInput.focus()
if (theTextArea) theTextArea.focus()
})
}
}
})
}
}
export default directiveObj
在main。js中全局注册就好了
import directiveObj from ‘./utils/directive’
Vue.use(directiveObj)
边栏推荐
- Determine a binary tree given inorder traversal and another traversal method
- 不同的操作加不同的锁详解
- 分享一个适用于MCU项目的代码框架
- 大整数相加,相减,相乘,大整数与普通整数的相乘,相除
- XSS靶场中级绕过
- PROE/Croe如何编辑已完成的草图,让其再次进入草绘状态
- Debug一个ECC的ODP数据源
- 【多任务模型】Progressive Layered Extraction: A Novel Multi-Task Learning Model for Personalized(RecSys‘20)
- 互联网大厂研发流程
- Greenplum数据库源码分析——Standby Master操作工具分析
猜你喜欢

网络不通?服务丢包?这篇 TCP 连接状态详解及故障排查,收好了~

XSS range intermediate bypass

Pytorch模型训练实用教程学习笔记:一、数据加载和transforms方法总结

【个人作品】记之-串口日志记录工具

openresty 动态黑白名单

第59章 ApplicationPart内置依赖注入中间件

【多任务学习】Modeling Task Relationships in Multi-task Learning with Multi-gate Mixture-of-Experts KDD18

Greenplum数据库源码分析——Standby Master操作工具分析

17. Load balancing

MySQL开发技巧——并发控制
随机推荐
Different operating with different locks, rounding
[Personal Work] Remember - Serial Logging Tool
[Multi-task optimization] DWA, DTP, Gradnorm (CVPR 2019, ECCV 2018, ICML 2018)
把 Oracle 数据库从 RAC 集群迁移到单机环境
部署zabbix
Get started quickly with MongoDB
有用的网站
Interpretation of the meaning of each dimension of two-dimensional, three-dimensional, and four-dimensional matrices
【nn.Parameter()】生成和为什么要初始化
大整数相加,相减,相乘,大整数与普通整数的相乘,相除
Redis 做签到统计
The graphic details Eureka's caching mechanism/level 3 cache
WhatsApp群发实战分享——WhatsApp Business API账号
Greenplum Database Source Code Analysis - Analysis of Standby Master Operation Tools
分享一个适用于MCU项目的代码框架
我的驾照考试笔记(1)
数值矩阵的图形表示
18、分布式配置中心nacos
MongoDB快速上手
数据库系统原理与应用教程(070)—— MySQL 练习题:操作题 101-109(十四):查询条件练习