当前位置:网站首页>自定义指令,获取焦点
自定义指令,获取焦点
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)
边栏推荐
猜你喜欢
随机推荐
【Redis】缓存雪崩、缓存穿透、缓存预热、缓存更新、缓存击穿、缓存降级
百度无人驾驶商业化已“上路”
解除360对默认浏览器的检测与修改
数据库系统原理与应用教程(070)—— MySQL 练习题:操作题 101-109(十四):查询条件练习
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
Pytorch模型训练实用教程学习笔记:三、损失函数汇总
多线程之生产者与消费者
Acrel-5010重点用能单位能耗在线监测系统在湖南三立集团的应用
用户体验好的Button,在手机上不应该有Hover态
regular expression
如何记录分析你的炼丹流程—可视化神器Wandb使用笔记【1】
启明云端分享|盘点ESP8684开发板有哪些功能
正则表达式
数值矩阵的图形表示
latex论文神器--服务器部署overleaf
AcWing 797. 差分
密码学的基础:X.690和对应的BER CER DER编码
【torch】张量乘法:matmul,einsum
1个小时!从零制作一个! AI图片识别WEB应用!
互联网大厂研发流程









