jQuery 对象不支持属性或方法“attachEvent” InternetExplorer 11
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28659450/
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
Object doesn't support property or method 'attachEvent' InternetExplorer 11
提问by student
If I press the form send button "Saada" in IE11, I will get an error:
如果我在IE11 中按下表单发送按钮“Saada” ,我会收到一个错误:
Object doesn't support property or method 'attachEvent'
I have found that this is an IE11problem, but the only solution I have found is to use my developer tools and set the browser to run in IE9mode for example.
我发现这是一个IE11问题,但我找到的唯一解决方案是使用我的开发人员工具并将浏览器设置为在IE9模式下运行。
But I want everyone to use my website without using their developer tools.
Do you know some other solution I could try. Or maybe I have to import some other Jquerylibraries?
但我希望每个人都可以在不使用他们的开发人员工具的情况下使用我的网站。
你知道我可以尝试的其他解决方案吗?或者我可能必须导入其他一些Jquery库?
Other browsers work fine, but this only happens in IE11
其他浏览器工作正常,但这只发生在 IE11
回答by Babar Sajjad
The javascript method attachEvent was replaced with the method addEventListener in IE11.
在 IE11 中,javascript 方法 attachEvent 被替换为 addEventListener 方法。
JQuery 1.10.1 still uses this method in case of IE > 8. This will cause javascript compilation errors.
JQuery 1.10.1 在IE > 8 的情况下仍然使用此方法。这会导致javascript 编译错误。
JQuery 1.10.2 seems to have solved this problem.
JQuery 1.10.2 似乎已经解决了这个问题。
Hope this will help
希望这会有所帮助
回答by Hymanson
attachEvent is a deprecated function used in older versions of Internet Explorer. For modern browsers use this instead.
attachEvent 是旧版本的 Internet Explorer 中不推荐使用的函数。对于现代浏览器,请改用它。
el.addEventListener(evt,func,false);
See documentation here
请参阅此处的文档
You could also create a function which checks which function to use
您还可以创建一个函数来检查要使用的函数
function addListener(el, event, func){
if (el.addEventListener) {
el.addEventListener(event, func, false);
}
else {
el.attachEvent("on"+event, func);
}
}
Then you can attach your event by doing this:
然后,您可以通过执行以下操作来附加您的活动:
var element = document.getElementById('myElement');
addListener(element, 'click', function(){
alert('You have clicked!');
});
If you are unable to to this then perhaps a polyfill will work instead. Try to insert this somewhere:
如果您无法做到这一点,那么也许 polyfill 会起作用。尝试将其插入某处:
if(!document.attachEvent){
Object.prototype.attachEvent=function(event,func){
this.addEventListener(event.split('on')[1], func);
}
}
Hope this helps
希望这可以帮助