IE9 Quirks模式不会将事件参数传递给事件处理程序 [英] IE9 Quirks mode doesn't pass event parameter to event handler
问题描述
我对IE9的怪癖模式有疑问.我已经将oncahnge事件注册到输入元素.到目前为止,该方法仍然有效,但是在IE9中,我们的网站进入了怪癖模式(保持原样),并且出现了问题,即浏览器没有将event-Object传递到我的处理程序方法中.
I have a problem with the IE9 quirks mode. I have registered an oncahnge-event to an input element. This works so far, but in IE9 our site goes to quirks mode (it is as it is) and there I have the problem, that the browser doesn't pass the event-Object into my handler method.
document.getElementById("foo").onchange=myChangeMethod;
function myChangeMethod(event){
//In IE 9 quirks mode "event" is undefined...
if(event != undefined){
//do stuff
}
}
可以通过打开WebDeveloper控制台(F12),将BrowserMode设置为"IE9"并将Document-Mode设置为(IE5-Quirks)在IE10中测试(我想在IE9中也可以).然后,浏览器的行为就像将本机IE9设置为"IE 9兼容模式",将文档模式设置为"Quirks"一样.
This can be tested in IE10 (i suppose als in IE9) with opening the WebDeveloper Console (F12), Setting BrowserMode to "IE9" and the Document-Mode to (IE5-Quirks). Then the browser behaves the same like setting the native IE9 to "IE 9 Compatibility mode" and Document-Mode to "Quirks".
是否有可能以某种方式获取事件对象?
Is there a possibility to get the event-Object somehow?
提前谢谢!
推荐答案
我认为该事件在IE怪癖模式下是全局事件,因此检查事件参数并分配window.event
是否为undefined
即可解决问题:>
I suppose the event is global in IE quirks mode, so checking the event parameter and assign window.event
if it's undefined
would solve the problem:
function myChangeMethod(evt){
//In IE 9 quirks mode "event" is undefined...
evt = evt || window.event; //<== HERE
// additionally in quirks mode evt.target is evt.srcElement,
// so if needed you could assign evt.target as:
var originator = evt.target || evt.srcElement;
if(evt){
//do stuff
}
}
这篇关于IE9 Quirks模式不会将事件参数传递给事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!