admin管理员组文章数量:1316835
I am creating some images in my page. When I write <img src=somesrc, onerror=othersrc></img>
, it works fine.
But when I create image using jQuery it fails:
jQuery('<img />', {
onerror: "javascript: this.src='some_default_image_source'",
src: 'image_source'}).appendTo("#container");
How do I solve this?
I am creating some images in my page. When I write <img src=somesrc, onerror=othersrc></img>
, it works fine.
But when I create image using jQuery it fails:
jQuery('<img />', {
onerror: "javascript: this.src='some_default_image_source'",
src: 'image_source'}).appendTo("#container");
How do I solve this?
Share Improve this question asked Jun 18, 2012 at 1:09 onemachonemach 4,3257 gold badges36 silver badges55 bronze badges 2- 1 But when I create image using jQuery it fails How does it fail? What exactly fails? – Felix Kling Commented Jun 18, 2012 at 1:33
- @FelixKling the onerror code does not get executed and so the default image does not get loaded. – onemach Commented Jun 18, 2012 at 3:34
2 Answers
Reset to default 7jQuery('<img />').error(function() {
this.src = 'some_default_image_source';
}).attr('src', 'image_source').appendTo("#container");
Works fine. Link to jsFiddle plus extra words
本文标签: javascriptcreate img with onerror attribute using jQueryStack Overflow
版权声明:本文标题:javascript - create img with onerror attribute using jQuery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742004507a2411687.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论