图片加载失败,加载默认

Posted 顺瓜摸藤

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了图片加载失败,加载默认相关的知识,希望对你有一定的参考价值。


<pre style="color:#000000;font-family:‘Consolas‘;font-size:15.0pt;"><br>
<span style=""><span style="">&lt;&lt;/span&gt;<span style="color:#000080;font-weight:bold;">img </span><span style="color:#0000ff;font-weight:bold;">src=</span><span style="color:#008000;font-weight:bold;">"&lt;{:config_img($var[‘img‘])}&gt;" </span><span style="color:#0000ff;font-weight:bold;">onerror=</span><span style="color:#008000;font-weight:bold;">"</span><span style="font-style:italic;">showImgDelay</span>(<span style="color:#000080;font-weight:bold;">this</span>,<span style="color:#008000;font-weight:bold;">‘‘</span>,<span style="color:#0000ff;">0</span>)<span style="color:#008000;font-weight:bold;">"</span><span style="">&gt;</span><br>
&lt;&lt;/span&gt;<span style="color:#000080;font-weight:bold;">script</span><span style="">&gt;</span><br>
    <span style="color:#000080;font-weight:bold;">function </span><span style="font-style:italic;">showImgDelay</span>(imgObj,imgSrc,maxErrorNum){<br>
<span style="color:#808080;font-style:italic;">//        showSpan.innerhtml += "--" + maxErrorNum;    //</span><span style="color:#808080;font-style:italic;font-family:‘宋体‘;">显示出加载图片出错的次数<br></span><span style="color:#808080;font-style:italic;font-family:‘宋体‘;">        </span><span style="color:#000080;font-weight:bold;">if</span>(maxErrorNum&gt;<span style="color:#0000ff;">0</span>){<br>
            imgObj.<span style="color:#7a7a43;">onerror</span>=<span style="color:#000080;font-weight:bold;">function</span>(){<br>
                <span style="font-style:italic;">showImgDelay</span>(imgObj,imgSrc,maxErrorNum-<span style="color:#0000ff;">1</span>);<br>
            };<br>
            <span style="color:#7a7a43;">setTimeout</span>(<span style="color:#000080;font-weight:bold;">function</span>(){<br>
                imgObj.<span style="color:#660e7a;font-weight:bold;">src</span>=imgSrc;<br>
            },<span style="color:#0000ff;">500</span>);<br>
        }<span style="color:#000080;font-weight:bold;">else</span>{<br>
            imgObj.<span style="color:#7a7a43;">onerror</span>=<span style="color:#000080;font-weight:bold;">null</span>;<br>
            imgObj.<span style="color:#660e7a;font-weight:bold;">src</span>=<span style="color:#008000;font-weight:bold;">"/attachs/default.jpg"</span>;<br>
        }<br>
    }<br>
<span style="">&lt;/&lt;/span&gt;<span style="color:#000080;font-weight:bold;">script</span><span style="">&gt;</span></span></span></span>
</pre>

以上是关于图片加载失败,加载默认的主要内容,如果未能解决你的问题,请参考以下文章

vue 图片加载失败时,加载默认图片

图片加载失败时,加载默认图片(未亲测)

JQuery图片加载显示loading和加载失败默认图片

js解决img标签加载失败显示默认图片

图片加载失败显示默认图片占位符

图片加载失败,加载默认