当前位置:网站首页>自定义指令,获取焦点
自定义指令,获取焦点
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)
边栏推荐
猜你喜欢
随机推荐
OSPO 五阶段成熟度模型解析
【kali-信息收集】(1.4)识别活跃的主机/查看打开的端口:Nmap(网络映射器工具)
Different operating with different locks, rounding
【Untitled】
【Untitled】
Win10, the middle mouse button cannot zoom in and out in proe/creo
PROE/Croe如何编辑已完成的草图,让其再次进入草绘状态
【多任务模型】Progressive Layered Extraction: A Novel Multi-Task Learning Model for Personalized(RecSys‘20)
58:第五章:开发admin管理服务:11:开发【管理员人脸登录,接口】;(未实测)(使用了阿里AI人脸识别)(演示了,使用RestTemplate实现接口调用接口;)
18、分布式配置中心nacos
工作5年,测试用例都设计不好?来看看大神的用例设计总结
Interpretation of the meaning of each dimension of two-dimensional, three-dimensional, and four-dimensional matrices
我的驾照考试笔记(4)
30-day question brushing plan (5)
不恰当Equatable协议==方法的实现对SwiftUI中@State修饰属性的影响
JS数组过滤
MongoDB快速上手
17. Load balancing
如何写一个vim插件?
小数据如何学习?吉大最新《小数据学习》综述,26页pdf涵盖269页文献阐述小数据学习理论、方法与应用









