onerror & lt; img>标签属性总是在IE中触发,为什么?

onerror <img> tag attribute always fires in IE, why?

本文关键字:IE 为什么 img lt 标签 onerror 属性      更新时间:2023-09-26

我有这样的代码

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

谢谢

我也经历过类似的症状。在我的情况下,通过在<img>src中放置'空'值发生'onerror'。

问题:

html

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

js

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

解决方案:

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

您可以这样尝试。首先,您必须编写一个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";           
        }

更改为:

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