Javascript onerror<;img>;标签属性总是在IE中激发,为什么?
我有这样的代码Javascript onerror<;img>;标签属性总是在IE中激发,为什么?,javascript,image,internet-explorer,onerror,Javascript,Image,Internet Explorer,Onerror,我有这样的代码 <a class="img" href="LINK"> <img src="GOOD_IMG" title="title" onerror="src='ERROR_IMG'"> </a> 然后出现警告消息并显示正确的图像 是什么原因导致IE在其他浏览器没有问题的情况下激活onerror 是否有什么方法可以找出导致错误的原因 感谢更改为: onerror = function() { alert('error'); }; 你
<a class="img" href="LINK">
<img src="GOOD_IMG" title="title" onerror="src='ERROR_IMG'">
</a>
然后出现警告消息并显示正确的图像
是什么原因导致IE在其他浏览器没有问题的情况下激活onerror
是否有什么方法可以找出导致错误的原因
感谢更改为:
onerror = function() {
alert('error');
};
你可以这样试试。首先,您必须编写一个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";
}
函数imgError(图像控制,路径){
$.ajax({
类型:“POST”,
async:true,
url:“test.aspx/CheckImageExists”,
数据:“{'imagePath':”+JSON.stringify(path)+“}”,
contentType:“应用程序/json;字符集=utf-8”,
成功:功能(响应){
如果(response.d==“存在”){
imageControl.src=路径;
}
否则{
imageControl.src='img/errorimg.jpg';
}
}
});
返回true;
}
“onerror=”imgError(这个,””>
C#
[网络方法]
公共静态字符串CheckImageExists(字符串imagePath)
{
字符串fullPath=HttpRuntime.AppDomainAppPath.ToString()+imagePath;
fullPath=fullPath.Replace(“/”,“\\”);
返回文件.Exists(完整路径)?“Exists”:“notexists”;
}
我也经历过类似的症状。
在我的例子中,“onerror”是通过在src
at
尝试将onerror
设置为类似myFunc(e)的值
并在控制台中记录e
在IE8和IE9中为我工作:可能是IE不识别特定的图像吗?我发现,如果它不能立即获得图像,它将触发onerror
。我们在应用程序中利用这一点,从数据动态设置配置文件图片的源基本,所以onerror
触发,我们将其设置为默认联系人图片,直到实际图片加载。MMM的方法是我想尝试的,但我不知道如何做到这一点,是否有人可以提供更多有关此问题的信息?(如果我只是照上面所说的那样做,那么我得到“e未定义”)具有完全相同的问题
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";
}
<img src="" onerror="this.src='ERROR_IMG'">
$('._profile_image:first').attr('src', IMAGE_URL);
<img onerror="this.src='ERROR_IMG'">