首页 > 其他分享 >document.activeElement

document.activeElement

时间:2022-10-18 00:22:35浏览次数:60  
标签:document textarea 元素 input div 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

标签:document,textarea,元素,input,div,activeElement
From: https://www.cnblogs.com/beileixinqing/p/16801201.html

相关文章