activeElement在safari与chrome总是返回body?

现在都支持document.activeElement属性 ,但是在safari与chrome出现了些状况……到stackoverflow查了一下,但给出的方法并不靠谱,因为click事件总是赶在focus事件前发生。

//http://stackoverflow.com/questions/483741/how-to-determine-which-html-page-element-has-focus
function _dom_trackActiveElement(evt) {
    if (evt && evt.target) { 
        document.activeElement = evt.target == document ? null : evt.target;
    }
}

function _dom_trackActiveElementLost(evt) { 
    document.activeElement = null;
}

if (!document.activeElement) {
    document.addEventListener("focus",_dom_trackActiveElement,true);
    document.addEventListener("blur",_dom_trackActiveElementLost,true);
}

在标准浏览器支持一个叫DOMActivate,但同样太慢了,帮不上忙。

注意,我的目的是想获取当前激活元素,并不是想得到当前的点击的元素……

记得我以前好像在背光博客见过这种机制的解析,但此博客在河蟹浪潮中覆灭了……

原文地址:https://www.cnblogs.com/rubylouvre/p/1659274.html