簡體   English   中英

img-tag 中 src 的 JS 邏輯

[英]JS logic for src in a img-tag

我正在從 API 獲取數據、文本和 imageUrl。 在沒有返回 imageUrl 的情況下,我想顯示我的資產文件夾中的圖像。

我可以在 img-tag 中編寫邏輯嗎?如果可以的話怎么寫? 還是我必須在外面寫一些 function ?

這是現在的代碼外觀:

 profiles.forEach((profile) => { expertiseContainer.innerHTML += ` <div > <img src="${ profile.imgUrl }" alt="Profile image"> <h2>${ profile.name }</h2> </div> ` })

下面是一種方法,在模板字符串中使用條件運算符:

 profiles.forEach((profile) => { expertiseContainer.innerHTML += ` <div > <img src="${ profile.imgUrl? profile.imgUrl: 'assets/default.png' }" alt="Profile image"> <h2>${ profile.name }</h2> </div> ` })

暫無
暫無

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

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