如何在IE9中访问Event.target? [英] How to access Event.target in IE9?

查看:197
本文介绍了如何在IE9中访问Event.target?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HTML DOM对象模型定义了一个事件对象的IE文档,而不是Windows发展。


The HTML DOM object model defines an Event object with a target property.

Looking at MSDN, Microsoft documents a target property. They also document srcElement as an alias of target from earlier versions of Internet Explorer:

The target property is similar to srcElement in Windows Internet Explorer 8 and earlier versions.


So here i am in Internet Explorer, sitting at a click breakpoint:

<div class="day" onclick="divClick(this)">

function divClick(sender)
{
   var divCell = sender;

And at the F12 Tools console i can ask for the global event object:

>> event 
{
    actionURL : "",
    altKey : false,
    altLeft : false,
    behaviorCookie : 0,
    behaviorPart : 0,
    bookmarks : null,
    boundElements : {...},
    button : 0,
    buttonID : 0,
    cancelBubble : false
    ...
} 

And i can ask for the event.srcElement object:

>> event.srcElement 
{
    align : "",
    noWrap : false,
    dataFld : "",
    dataFormatAs : "",
    dataSrc : "",
    currentStyle : {...},
    runtimeStyle : {...},
    accessKey : "",
    className : "header",
    contentEditable : "inherit"
    ...
} 

But event.target is empty:

>> event.target 

And if i watch event, there is no target property:

So how do i access the target property of an event object in Internet Explorer (9 (Document Mode: IE9 Standards (Browser Mode: IE9)))?

解决方案

If you want to use event.target in IE9, you'll need to use addEventListener()-method to assign eventhandler to the element.

<div id="day" class="day"></div>

document.getElementById('day').addEventListener('click',divClick,false);

function divClick(e){
   alert(e.target);
   divCell=this;
   :
}

In divClick() you can refer day simply using keyword this. Argument e contains a reference to the event-object itself.

BTW, in MSDN you can find maybe more suitable IE-documentation for Web development instead of Windows development.

这篇关于如何在IE9中访问Event.target?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆