閱讀177 返回首頁    go 阿裏雲 go 技術社區[雲棲]


圖片onerror事件,為圖片加載指定默認圖片

為圖片指定加載失敗時顯示默認圖片,js輸出的img對象,onerror是事件,不是屬性,所以這樣寫是不起作用的:
var img = $(document.createElement("IMG"));
    img.attr({
        "src": imgs[idx],
        "alt": tips[idx],
        "onerror":"this.src='" + NoPicPath + "'"
    }).appendTo(div);

應該是綁定事件:
    //圖片加載失敗時,加載默認圖片
    $('img').error(function () {
      $(this).attr('src', NoPicPath);
    });
    if ($.browser.msie && $.browser.version < 9) {
        $('img').each(function () {
           $(this).attr('src', $(this).attr('src'));
        });
    }

參考:https://www.paulund.co.uk/handle-image-loading-errors-with-jquery

最後更新:2017-04-03 16:48:50

  上一篇:go 華為----大有所“為”
  下一篇:go Activity的intent詳解~