手机
当前位置:查字典教程网 >网页设计 > Div+Css教程 >event.currentTarget和document.activeElement用法
event.currentTarget和document.activeElement用法
摘要:firefox的event.currentTarget和ie的document.activeElement这两个完全含义不一致的对象却被捆绑...

firefox 的 event.currentTarget 和 ie 的 document.activeElement 这两个完全含义不一致的对象却被捆绑在一起做成了浏览器兼容代码。 大家在google搜索的时候 都会发现 document.activeElement || event.currentTarget 的用法。

其实这样的用法是错误的,因为这两个属性的意义都不一样。

event.currentTarget

Identifies the current target for the event, as the event traverses the DOM.

识别当前事件触发的dom

document.activeElement

Gets the object that has the focus when the parent document has focus.

获得当前focus的对象

例子 <script type="text/javascript">

var show = function (e) {

alert(e.currentTarget?"获取到了 currentTarget":"获取到了 document.activeElement")

alert((e.currentTarget || document.activeElement).tagName);

};

</script><BR> 案例1: <br/><BR> <a href="#"><img src="http://www.google.cn/images/nav_logo3.png" border="0" alt="" onclick="show(event)"/></a> <br/> <br/><BR> 案例2: <br/><BR> <a href="#" onclick="show(event)"><img src="http://www.google.cn/images/nav_logo3.png" border="0" alt=""/></a> <br/><P></P>

<P> 这两个案例,在ie和firefox下试验一下。</P>

最后测试补充:后来测试了一下 document.activeElement 的支持程度,发现四大浏览器safari除外, ie firefoxopera都提供了这个对象的支持。但是有点需要注意的,上面的例子中 opera 会把图片作为 可以 focus的对象。导致document.activeElement的结果不一致...

【event.currentTarget和document.activeElement用法】相关文章:

CSS中使用counter()在列表中自动添加序号

IE6下div层被select控件遮住的问题解决方法

WEB标准布局的Div + CSS 高度自适应解决方法

CSS中的!important属性用法

css3实现input输入框颜色渐变发光效果代码

学习CSS的伪类

高效整洁CSS代码

让内层将外层撑开

用CSS设置表格边框的语法详解

div使用margin:0px auto不居中的原因分析及解决

精品推荐
分类导航