亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在html中連接img src

如何在html中連接img src

搖曳的薔薇 2023-10-24 21:20:03
我的目標是創建一個包含許多圖像的表格。通過這樣做,我將遍歷一個包含該圖像位置的數組。我的問題是,如何將此字符串轉換為動態字符串: <td><img src="http://openweathermap.org/img/w/ + local.weather[0].icon +.png"></td>“local.weather[0].icon”是動態部分。我似乎無法找到一種方法來實現這一點。
查看完整描述

2 回答

?
大話西游666

TA貢獻1817條經驗 獲得超14個贊

使用模板字符串。


here is the example,

var val = 'fortnite'

var x = 'I play ${val}';

console.log(x);     // I play fortnite

val = 'pubg'

console.log(x);     // I play pubg

//so I have made that dynamic using template strings. Use ` key instead for quotes.

[![This is the key][1]][1]


查看完整回答
反對 回復 2023-10-24
?
汪汪一只貓

TA貢獻1898條經驗 獲得超8個贊

你必須在js中手動完成它。(您無法在頁面加載時動態添加 src 屬性。為此,您必須在服務器上執行此操作或在窗口加載時通過 js 更改它)嘗試下面的代碼。

<td><img id="weatherImg"></td>
window.addEventListener('load', () => {
    document.getElementById("weatherImg").src = `http://openweathermap.org/img/w/${local.weather[0].icon}.png`;
});


查看完整回答
反對 回復 2023-10-24
  • 2 回答
  • 0 關注
  • 126 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號