javascript IE 11 调度事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27643181/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
IE 11 DispatchEvent
提问by JohnnyCage
We have a datepicker (in JavaScript) that has a section for checking IE 8 and older and other modern browsers.
我们有一个日期选择器(在 JavaScript 中),其中有一个部分用于检查 IE 8 及更早版本和其他现代浏览器。
if(-1 != navigator.userAgent.indexOf("MSIE")){
obj_caller.target.fireEvent("onchange");
}
else {
var evt = document.createEvent("HTMLEvents");
evt.initEvent("change", false, true);
obj_caller.target.dispatchEvent(evt);
}
It's working fine in Chrome, Firefox, IE8 and below but is failing in IE 11. What I need is a way to get the else part working in IE 11. I just cannot figure out what is failing and how to fix it.
它在 Chrome、Firefox、IE8 及更低版本中运行良好,但在 IE 11 中失败。我需要的是一种让 else 部分在 IE 11 中工作的方法。我只是无法弄清楚什么是失败的以及如何修复它。
Thanks.
谢谢。
采纳答案by Ashley Lee
Your problem is that fireEvent shouldn't be used in newer IE versions. Support for dispatchEvent was added in IE9. http://help.dottoro.com/ljrinokx.php
您的问题是不应在较新的 IE 版本中使用 fireEvent。在 IE9 中添加了对 dispatchEvent 的支持。http://help.dottoro.com/ljrinokx.php
if(document.createEventObject) {
obj_caller.target.fireEvent("onchange");
} else {
var evt = document.createEvent("HTMLEvents");
evt.initEvent("change", false, true);
obj_caller.target.dispatchEvent(evt);
}