【问题标题】:placeholder image for background-image?背景图像的占位符图像?
【发布时间】: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 作为占位符,但这似乎不适用于背景图像。有什么建议么?

【问题讨论】:

    标签: html css pug


    【解决方案1】:
    <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)');
    });
    

    【讨论】:

      【解决方案2】:

      你需要this.style.backgroundImage不是this.style.background-image

      【讨论】:

        【解决方案3】:

        您不能在 &lt;div&gt; 上使用 onerror。 onerror 只能用于&lt;img&gt;, &lt;object&gt;, &lt;script&gt;, &lt;style&gt;。所以,需要用&lt;img&gt;标签替换。如果你不想替换它, 我在stackoverflow上找到了。 How to replace nonexistent images with a placeholder in asynchronous loading from database

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-09-25
          • 2018-05-19
          • 1970-01-01
          • 1970-01-01
          • 2022-11-08
          • 2016-12-02
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多