查看完整版本: event.currentTarget和document.activeElement

琦琦儿 2008-10-6 10:15

event.currentTarget和document.activeElement

firefox 的 event.currentTarget 和 ie 的 document.activeElement 这两个完全含义不一致的对象却被捆绑在一起做成了浏览器兼容代码。 大家在[color=#51b5d5]google搜索[/color]的时候 都会发现 document.activeElement || event.currentTarget 的用法。
[color=#ff0000]其实这样的用法是错误的,因为这两个属性的意义都不一样。[/color]
[b][color=#51b5d5]event.currentTarget[/color][/b]
Identifies the current target for the event, as the event traverses the DOM.
识别当前事件触发的dom
[b][color=#51b5d5]document.activeElement[/color][/b]
Gets the object that has the focus when the parent document has focus.
获得当前focus的对象
[b]例子[/b]
<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="" /></a> <br/> <br/><BR>  案例2: <br/><BR>  <a href="#" ><img src="http://www.google.cn/images/nav_logo3.png" border="0" alt=""/></a> <br/><P></P>  
<P>  这两个案例,在ie和firefox下试验一下。</P>
[b]最后测试补充:[/b]后来测试了一下 document.activeElement  的支持程度,发现四大浏览器safari除外, ie firefox opera都提供了这个对象的支持。[color=#ff0000][b]但是[/b][/color]有点需要注意的,上面的例子中 opera 会把图片作为 可以 focus的对象。导致document.activeElement的结果不一致...
页: [1]
查看完整版本: event.currentTarget和document.activeElement