当前位置:网站首页>自定义指令,获取焦点
自定义指令,获取焦点
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)
边栏推荐
猜你喜欢

数据可视化

【kali-信息收集】(1.4)识别活跃的主机/查看打开的端口:Nmap(网络映射器工具)

我的驾照考试笔记(4)

Greenplum Database Source Code Analysis - Analysis of Standby Master Operation Tools

Greenplum数据库源码分析——Standby Master操作工具分析
![58: Chapter 5: Develop admin management services: 11: Develop [admin face login, interface]; (not measured) (using Ali AI face recognition) (demonstrated, using RestTemplate to implement interface cal](/img/ab/1c0adeb344329e28010b6ffda5389d.png)
58: Chapter 5: Develop admin management services: 11: Develop [admin face login, interface]; (not measured) (using Ali AI face recognition) (demonstrated, using RestTemplate to implement interface cal

正则表达式

57:第五章:开发admin管理服务:10:开发【从MongoDB的GridFS中,获取文件,接口】;(从GridFS中,获取文件的SOP)(不使用MongoDB的服务,可以排除其自动加载类)

八百客、销售易、纷享销客各行其道

Does LabVIEW really close the COM port using VISA Close?
随机推荐
【kali-信息收集】(1.4)识别活跃的主机/查看打开的端口:Nmap(网络映射器工具)
Intranet penetration lanproxy deployment
【节能学院】智能操控装置在高压开关柜的应用
Software you should know as a programmer
57:第五章:开发admin管理服务:10:开发【从MongoDB的GridFS中,获取文件,接口】;(从GridFS中,获取文件的SOP)(不使用MongoDB的服务,可以排除其自动加载类)
Win10, the middle mouse button cannot zoom in and out in proe/creo
【webrtc】sigslot : 继承has_slot 及相关流程和逻辑
解除360对默认浏览器的检测与修改
【Untitled】
How PROE/Croe edits a completed sketch and brings it back to sketching state
数据库系统原理与应用教程(070)—— MySQL 练习题:操作题 101-109(十四):查询条件练习
1个小时!从零制作一个! AI图片识别WEB应用!
MySQL你到底都加了什么锁?
Pytorch模型训练实用教程学习笔记:四、优化器与学习率调整
PROE/Croe如何编辑已完成的草图,让其再次进入草绘状态
数据可视化
17. Load balancing
Acrel-5010重点用能单位能耗在线监测系统在湖南三立集团的应用
研究生新同学,牛人看英文文献的经验,值得你收藏
小数据如何学习?吉大最新《小数据学习》综述,26页pdf涵盖269页文献阐述小数据学习理论、方法与应用