【发布时间】:2014-04-21 05:13:59
【问题描述】:
.mydiv(style="background-image: url('"+pic+"');" onerror="this.style.background-image:url('http://www.naijaticketshop.com/images/default_profile.jpg'); ")
我知道您会使用 onerror 作为占位符,但这似乎不适用于背景图像。有什么建议么?
【问题讨论】:
.mydiv(style="background-image: url('"+pic+"');" onerror="this.style.background-image:url('http://www.naijaticketshop.com/images/default_profile.jpg'); ")
我知道您会使用 onerror 作为占位符,但这似乎不适用于背景图像。有什么建议么?
【问题讨论】:
<img src="my.png" onerror="this.src = 'http://www.naijaticketshop.com/images/default_profile.jpg';" />
或使用 jquery
// Replace source
$('.mydiv').error(function(){
$(this).attr('background-image', 'url(http://www.naijaticketshop.com/images/default_profile.jpg)');
});
【讨论】:
你需要this.style.backgroundImage,不是this.style.background-image
【讨论】:
您不能在 <div> 上使用 onerror。 onerror 只能用于<img>, <object>, <script>, <style>。所以,需要用<img>标签替换。如果你不想替换它,
我在stackoverflow上找到了。 How to replace nonexistent images with a placeholder in asynchronous loading from database
【讨论】: