【问题标题】:How to detect IE 10+ for an onerror event如何检测 IE 10+ 的 onerror 事件
【发布时间】:2016-12-23 22:54:53
【问题描述】:

我在 IE10+ 上缩放 SVG 时遇到了一点问题。 我有这个代码:

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

如何检测 IE10+ 的 onerror 事件?

问候

【问题讨论】:

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


    【解决方案1】:

    对于 IE10+,使用微软特有的window.msMatchMedia 功能检测:

    function handler(e)
      {
      if (!!window.msMatchMedia)
        {
        e.target.src = "http://stackoverflow.com/favicon.ico";
        }
      }
    
    document.querySelector("#ie10_plus").addEventListener("error", handler, false)
    &lt;img src="" id="ie10_plus" alt=""&gt;

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

    document.querySelector('#foo').src="//foo";
    <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

    参考文献

    【讨论】:

    • 谢谢你的消息。但我应该检测浏览器,而不是尝试在 ie 上强制错误以加载 png。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-27
    • 1970-01-01
    • 1970-01-01
    • 2012-06-25
    • 1970-01-01
    相关资源
    最近更新 更多