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,但同样太慢了,帮不上忙。

<!doctype html> <html dir="ltr" lang="zh-CN"> <head> <meta charset="utf-8"/> <title>activeElement by 司徒正美</title> <script type="text/javascript"> window.onload = function(){ var safari=/a/.__proto__=='//'; //http://www.quirksmode.org/js/detect.html var chrome= navigator.userAgent.indexOf("Chrome") !== -1; var addEvent = (function () { if (document.addEventListener) { return function (el, type, fn) { el.addEventListener(type, fn, false); }; } else { return function (el, type, fn) { el.attachEvent('on' + type, function () { return fn.call(el, window.event); }); } } })(); function getActiveElement(e) { var m = document.activeElement; alert(m) } var inputs = document.getElementsByTagName("input"); for(var i=0,n=inputs.length;i<n;i++){ addEvent(inputs[i],"click",getActiveElement) } } </script> </head> <body> <form action="#"> <input type="checkbox" name="team" value="team" checked>Spurs<br> <input type="checkbox" name="team" value="pacers">Pacers<br> <input type="checkbox" name="team" value="kings" >Kings<br> <input type="checkbox" name="team" value="other">Other <p> <input type="button" id="aa" value="按扭1" > </p> <p> <input type="button" id="bb" value="按扭2" > </p> <p> <input type="button" id="cc" value="按扭3" > </p> </form> </body> </html>

运行代码

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

<!doctype html> <html dir="ltr" lang="zh-CN"> <head> <meta charset="utf-8"/> <title>activeElement by 司徒正美</title> <script type="text/javascript"> window.onload = function(){ var safari=/a/.__proto__=='//'; //http://www.quirksmode.org/js/detect.html var chrome= navigator.userAgent.indexOf("Chrome") !== -1; var addEvent = (function () { if (document.addEventListener) { return function (el, type, fn) { el.addEventListener(type, fn, false); }; } else { return function (el, type, fn) { el.attachEvent('on' + type, function () { return fn.call(el, window.event); }); } } })(); function getActiveElement(e) { var m; if(safari || chrome){ m = e.target == document ? null : e.target; }else{ m = document.activeElement; } alert(m) } var inputs = document.getElementsByTagName("input"); for(var i=0,n=inputs.length;i<n;i++){ addEvent(inputs[i],"click",getActiveElement) } } </script> </head> <body> <form action="#"> <input type="checkbox" name="team" value="team" checked>Spurs<br> <input type="checkbox" name="team" value="pacers">Pacers<br> <input type="checkbox" name="team" value="kings" >Kings<br> <input type="checkbox" name="team" value="other">Other <p> <input type="button" id="aa" value="按扭1" > </p> <p> <input type="button" id="bb" value="按扭2" > </p> <p> <input type="button" id="cc" value="按扭3" > </p> </form> </body> </html>

运行代码

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值