简体   繁体   English

JQuery error()函数在IE中不起作用

[英]JQuery error() function not working in IE

I have the following image element that it's src does not exists. 我有以下图像元素,它的src不存在。 I want to use the jquery error function to detect if it has not loaded and replace the src with a generic image that I know exists. 我想使用jquery错误函数来检测它是否已加载,并将src替换为我知道存在的通用映像。 This works in chrome and firefox but on in IE. 这适用于chrome和firefox但在IE中。 Why does this not work in IE and are there any workarounds? 为什么这在IE中不起作用,是否有任何变通方法? Thanks! 谢谢!

<img id="main" src="missing-image.jpg" />

<script type="text/javascript">

    $(function () {
        $("#main").error(function () {
            $("#main").attr("src", "generic.jpg");
        });
    });

</script>

Timing issue? 时间问题?

DEMO HERE 在这里演示

<img id="mainImage" src="placeholder.jpg" />

<script type="text/javascript">
$(document).ready(function() {
  $("#mainImage").error(function () {
    $(this).attr("src", "generic.jpg");
  });
  $("#mainImage").attr("src","possibly_missing_image.jpg");
});

</script>

I ran into the same problem with ie and setting the img src to itself allowed enough time for ie to catch the image error 我遇到了与ie相同的问题并将img src设置为自己允许足够的时间来捕获图像错误

$(document).ready(function() {
  $("#mainImage").error(function () { 
    $(this).attr("src", "generic.jpg");
  })
  .each(function() {
  $(this).attr("src",$(this).attr("src"));
  });
 });

Try this 试试这个

$(function () {
        $("#main").bind('error abort', function () {
            $(this).attr("src", "generic.jpg");
        });
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM