如何检测IE 10+的onerror事件

时间:2016-03-10 10:24:57

标签: javascript html internet-explorer-11 internet-explorer-10 onerror

我在IE10 +上扩展SVG时遇到了一些问题。 我有这段代码:

<img src="img/logo.svg" alt="" onerror="this.src='img/logo.png'; this.onerror=null;">

如何检测IE10 +的onerror事件?

问候

1 个答案:

答案 0 :(得分:0)

对于IE10 +,请使用Microsoft特定的window.msMatchMedia功能检测:

&#13;
&#13;
function handler(e)
  {
  if (!!window.msMatchMedia)
    {
    e.target.src = "http://stackoverflow.com/favicon.ico";
    }
  }

document.querySelector("#ie10_plus").addEventListener("error", handler, false)
&#13;
<img src="" id="ie10_plus" alt="">
&#13;
&#13;
&#13;

通常,使用无效的src值来触发错误。例如:

&#13;
&#13;
document.querySelector('#foo').src="//foo";
&#13;
<img src="" width="16" height="16" onclick="javascript:this.src='http://www.stackexchange.com/favicon.ico'"/>Click Me

<img src="favicon.ico" onerror="javascript:this.src='http://stackoverflow.com/favicon.ico'" width="16" height="16"/>Despicable Me

<img id="foo" src="" width="16" height="16" onerror="javascript:this.src='http://stackoverflow.com/favicon.ico'"/>Despicable Me 2
&#13;
&#13;
&#13;

<强>参考