Event.target issue with Firefox 6
In Firefox 6 I tried to get the target element on which the event occurred, 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 fo开发者_Go百科r 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);
}
精彩评论