document.activeElement

document.activeElement获取当前获得焦点的元素:

IE:document.activeElement可获得所有聚焦的元素,包括input、textarea、div等。IE只关心光标聚焦的位置,不关心聚焦元素的性质。

chrome:document.activeElement仅对input、textarea等标准的输入文本有效;对于div等非编辑类的元素(即使开启了contentEditable),返回的值为BODY。

fireFox:document.activeElement可获得所有聚焦的元素。包括input、textarea、div等。

document.querySelector('body').onclick = function () {
   console.log(document.activeElement.tagName); //INPUT BODY BUTTON 获取标签名
   if (document.activeElement.tagName == 'BUTTON') { //若为指定的元素,则进行相应的操作
      window.location.href = "http://www.baidu.com"
   }
}

转载自链接:https://blog.csdn.net/qq_45695853/article/details/119142867

posted @   蓓蕾心晴  阅读(200)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
历史上的今天:
2019-10-18 overflow-x:scroll失效问题解决
点击右上角即可分享
微信分享提示