本文介绍了对象不支持属性或方法'attachEvent'InternetExplorer 11的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在 IE11 中按下发送按钮Saada,我会收到错误消息:

  

我发现这是一个 IE11 问题,但我找到的唯一解决方案是使用我的开发人员工具并将浏览器设置为以 IE9 模式运行。



但是我希望每个人都可以使用我的网站,而无需使用他们的开发工具。

您是否知道我可以尝试的其他解决方案。或者,也许我必须导入一些其他的 Jquery 库?

其他浏览器正常工作,但这只发生在IE11中

解决方案

attachEvent是旧版Internet Explorer中使用的弃用函数。
对于现代浏览器而言,使用它。

  el.addEventListener(evt,func,false); 

查看文档 here



您也可以创建一个函数来检查使用哪个函数



  function addListener(el,event,func){
if(el.addEventListener){
el.addEventListener (event,func,false);
}
else {
el.attachEvent(on+ event,func);


$ / code>

然后你可以这样做来附加你的事件:

  var element = document.getElementById('myElement'); 
addListener(element,'click',function(){
alert('You have clicked!');
});

如果您无法做到这一点,那么也许可以使用polyfill。尝试将其插入某处:

  if(!document.attachEvent){
Object.prototype.attachEvent = function(事件,功能){
this.addEventListener(event.split('on')[1],func);
}
}

希望这有助于


If I press the form send button "Saada" in IE11, I will get an error:

Object doesn't support property or method 'attachEvent'

I have found that this is an IE11 problem, but the only solution I have found is to use my developer tools and set the browser to run in IE9 mode for example.

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 Jquery libraries?

Other browsers work fine, but this only happens in IE11

解决方案

attachEvent is a deprecated function used in older versions of Internet Explorer.For modern browsers use this instead.

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:

if(!document.attachEvent){
  Object.prototype.attachEvent=function(event,func){
    this.addEventListener(event.split('on')[1], func);
  }
}

Hope this helps

这篇关于对象不支持属性或方法'attachEvent'InternetExplorer 11的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 01:31