开发者

JQuery error() function not working in IE

开发者 https://www.devze.com 2023-03-23 16:09 出处:网络
I have the following image element that it\'s src does not exists.I want to use the jquery error function to detect if it has not lo开发者_如何学编程aded and replace the src with a generic image that

I have the following image element that it's src does not exists. I want to use the jquery error function to detect if it has not lo开发者_如何学编程aded and replace the src with a generic image that I know exists. This works in chrome and firefox but on in IE. Why does this not work in IE and are there any workarounds? 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

$(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");
        });
    });
0

精彩评论

暂无评论...
验证码 换一张
取 消