简体   繁体   English

Firefox 6的Event.target问题

[英]Event.target issue with Firefox 6

In firefox 6 I tried to get the target element on which the event occured, but it does not show any element and it shows undefined in alert. 在firefox 6中,我试图获取事件发生的目标元素,但它没有显示任何元素,并且它在警报中显示未定义。 Tried to debug it using the firebug tool and found the attribute "target" missing for the event object. 试图使用firebug工具调试它,并发现事件对象缺少属性“target”。 Can anyone help me out? 谁能帮我吗? I do have the code below 我有以下代码

function getSource(event)
{
    if(!event) 
    { 
        field = window.event.srcElement;
       alert(field);
    }
    else
    {
        field = event.target; 
        alert(field) //Getting undefined in FF6
    }
}

Edited Portion 编辑部分

document.onkeypress = getSource;
document.onmouseup = getSource;

Any help would be appreciated. 任何帮助,将不胜感激。

Try the code below 请尝试下面的代码

function getSource(e)
{
     if(!e)
        e = window.event;
     field = evt.srcElement || evt.target;
     alert(field);
     return true;
 } 

Hope this helps you. 希望这对你有所帮助。

Test this in Fx 6: 在Fx 6中测试:

<script type="text/javascript">

window.onload = function() {
  document.getElementById('d0').onclick = showTarget;
}

function showTarget(e) {
  e = e || window.event;
  var target = e.target || e.srcElement;
  alert(target.tagName);
}

</script>

<div id="d0">
  <p>click on me</p>
</div>

It should alert "P". 它应警告“P”。

As also explained in the similar question, change the function to this: 同样在类似的问题中解释,将功能更改为:

function getSource(evt)
{
    if(!evt) 
        evt = window.event;
    if (evt) {
        field = evt.srcElement || evt.target;
        alert(field);
        return true;
    }
    alert("event not found");
    return false;
}
function getSource(ev) {
  var el=(ev=ev||window.event).target||ev.srcElement;
  alert(el+" "+el.tagName);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM