【问题标题】:onerror <img> tag attribute always fires in IE, why?onerror <img> 标签属性总是在 IE 中触发,为什么?
【发布时间】:2013-04-01 04:46:37
【问题描述】:

我有类似这样的代码

<a class="img" href="LINK">
  <img src="GOOD_IMG" title="title" onerror="src='ERROR_IMG'">
</a>

在 FireFox 和 chrome 中,它的行为与您预期的一样(如果存在则显示 GOOD_IMG,如果不存在则显示 ERROR_IMG),但在 IE (9) 中,它始终显示 ERROR_IMG。

如果我在 IE 中调试并在运行中设置 onerror 等等其他的东西,例如

onerror="alert('error')" 

然后出现警告消息并显示正确的图像。

什么可能导致 IE 导致 onerror 在其他浏览器没有问题的情况下激活?

有什么方法可以找出导致onerror 的原因吗?

谢谢

【问题讨论】:

  • 尝试将onerror 设置为myFunc(e) 并在控制台中记录e
  • 在 IE8 和 IE9 中为我工作:jsfiddle.net/LyZmq 难道是 IE 无法识别该特定图像?
  • 我发现如果它不能立即获取图像,它会触发onerror。我们在我们的应用程序中利用这一优势,从数据库中动态设置个人资料图片的来源,因此onerror 触发,我们将其设置为默认联系人图片,直到实际图片加载。
  • MMM 的方法是我想尝试的,但我不知道该怎么做,有人可以提供更多信息吗? (如果我只是按照所说的做,那么我会得到“e is undefined”)
  • 遇到同样的问题

标签: javascript image internet-explorer onerror


【解决方案1】:

改为:

onerror = function() { 
    alert('error'); 
}; 

【讨论】:

    【解决方案2】:

    你可以这样试试。首先,您必须编写一个 JS 函数来检查图像是否存在(AJAX 调用返回是否存在)并相应地更改图像。

    第二次你在 onerror 事件上调用函数

    function imgError(imageControl, path) {        
                $.ajax({
                    type: "POST",
                    async: true,
                    url: "test.aspx/CheckImageExists",
                    data: "{'imagePath':" + JSON.stringify(path) + "}",
                    contentType: "application/json; charset=utf-8",
                    success: function (response) {
                        if (response.d == "exists") {
                            imageControl.src = path;
                        }
                        else {
                            imageControl.src = 'img/errorimg.jpg';
                        }
                    }
                });
                return true;
            }
    
    <img src="<%# "admin/"+ Eval("imagath") %>" onerror="imgError(this,'<%# "admin/"+ Eval("imagath") %>')">
    
    C#
     [WebMethod]       
            public static string CheckImageExists(string imagePath)
            {
                string fullPath = HttpRuntime.AppDomainAppPath.ToString() + imagePath;
                fullPath=fullPath.Replace('/','\\');
                return File.Exists(fullPath) ? "exists" : "notexists";           
            }
    

    【讨论】:

      【解决方案3】:

      我也经历过类似的症状。 在我的例子中,'onerror' 是通过在src &lt;img&gt; 中输入'empty' 值而发生的。

      问题:

      html

      <img src="" onerror="this.src='ERROR_IMG'">
      

      js

      $('._profile_image:first').attr('src', IMAGE_URL);
      

      解决方法:

      <img onerror="this.src='ERROR_IMG'">
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-12-02
        • 1970-01-01
        • 2013-07-12
        • 2019-07-25
        • 2022-06-24
        • 1970-01-01
        • 2021-08-11
        相关资源
        最近更新 更多