我的目標是創建一個包含許多圖像的表格。通過這樣做,我將遍歷一個包含該圖像位置的數組。我的問題是,如何將此字符串轉換為動態字符串: <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]

汪汪一只貓
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`; });
- 2 回答
- 0 關注
- 126 瀏覽
添加回答
舉報
0/150
提交
取消