Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
430 views
in Technique[技术] by (71.8m points)

javascript - Why Firefox says that window.event is undefined? (call function with added event listener)

I have a trouble in this part:

var ex = {
  exampl: function(){
    var ref=window.event.target||window.event.srcElement; // here
    alert(ref.innerHTML); // (example)
  }
}

This function is called this way:

document.body.childNodes[0].addEventListener('mouseover',ex.exampl,true);

Only Firefox says that window.event isn't defined...

I don't know what to do, to make it work. It works very well in webkit browsers and opera (I can't check it in MSIE and I don't care about it).

Why does it happen?

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

try getting the event using the parameter passed (named e in this case). i tested this and both window.event and the e is supported in chrome.

try checking for both, whichever exists

var ex = {
  exampl: function(e){

    console.log(window.event);
    console.log(e);  

    //check if we have "e" or "window.event" and use them as "evt"
    var evt = e || window.event    

  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...