簡體   English   中英

如果圖像不存在,請用div替換圖像

[英]Replace image with div if image doesn't exists

如何正確反映圖像(如果存在)並用div替換?

// I don't know how to get `imageExists` without making cross-origin
(imageExists) ? (<img class="avatar-img" src={this.state.imageDataUrl}/>) : 
                     (<div class="avatar-img no-avatar"><span>{shortcut}</span></div>)

當然,我可以單獨做XmlHttpRequest但這有很多不必要的代碼+跨域。

使用onerror事件捕獲所有無法加載的圖像,並用所需的<div>替換它們:

$('img').on('error', function() { 
    $(this).replaceWith('<div class="avatar-img no-avatar"><span>{shortcut}</span></div>');
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM