简体   繁体   中英

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. Tried to debug it using the firebug tool and found the attribute "target" missing for the event object. 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:

<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".

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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