.mydiv(style="background-image: url('"+pic+"');" onerror="this.style.background-image:url('http://www.naijaticketshop.com/images/default_profile.jpg'); ")

I'm aware you would use onerror for an placeholder, but this doesn't seem to be working for background-image. Any suggestions?

有帮助吗?

解决方案

<img src="my.png" onerror="this.src = 'http://www.naijaticketshop.com/images/default_profile.jpg';" />

or with jquery

// Replace source
$('.mydiv').error(function(){
        $(this).attr('background-image', 'url(http://www.naijaticketshop.com/images/default_profile.jpg)');
});

其他提示

You need this.style.backgroundImage, not this.style.background-image

You can't use onerror on <div>. onerror can only use on <img>, <object>, <script>, <style>. So, Need to replace with <img> tag. If you don't want to replace with it, I found that on stackoverflow. How to replace nonexistent images with a placeholder in asynchronous loading from database

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top