我的目標是創(chuàng)建一個包含許多圖像的表格。通過這樣做,我將遍歷一個包含該圖像位置的數(shù)組。我的問題是,如何將此字符串轉(zhuǎn)換為動態(tài)字符串: <td><img src="http://openweathermap.org/img/w/ + local.weather[0].icon +.png"></td>“l(fā)ocal.weather[0].icon”是動態(tài)部分。我似乎無法找到一種方法來實現(xiàn)這一點。
2 回答

大話西游666
TA貢獻1817條經(jīng)驗 獲得超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]

汪汪一只貓
TA貢獻1898條經(jīng)驗 獲得超8個贊
你必須在js中手動完成它。(您無法在頁面加載時動態(tài)添加 src 屬性。為此,您必須在服務器上執(zhí)行此操作或在窗口加載時通過 js 更改它)嘗試下面的代碼。
<td><img id="weatherImg"></td>
window.addEventListener('load', () => { document.getElementById("weatherImg").src = `http://openweathermap.org/img/w/${local.weather[0].icon}.png`; });
- 2 回答
- 0 關(guān)注
- 134 瀏覽
添加回答
舉報
0/150
提交
取消