admin 管理员组文章数量: 1087134
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 loaded 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>
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 loaded 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>
Share
Improve this question
asked Jul 29, 2011 at 14:02
PhilPhil
4,2244 gold badges25 silver badges40 bronze badges
2
- 2 take a look an similar question stackoverflow./questions/263359/… – Carlos Co Commented Jul 29, 2011 at 14:11
- It looks like jquery ignores .error() in IE if there is no file extension on the source. I had to use the workaround Carlos mentions in the ment above. – Buchannon Commented Jun 11, 2012 at 19:43
3 Answers
Reset to default 3Timing 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");
});
});
本文标签: javascriptJQuery error() function not working in IEStack Overflow
版权声明:本文标题:javascript - JQuery error() function not working in IE - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744031577a2521599.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论