document.activeElement获取当前获得焦点的元素,在chrome总是得到body,后来经过测试得到结果如下:
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"
}
}