這是成功顯示圖像的完整url。如何將JSON圖像值分配給HTML img標記
http://openweathermap.org/img/w/04d.png
注意,除了JSON屬性「圖標」規定值「04D」,它是由我來把那爲URL才能使用它。
只是作爲參考,以表明JSON路徑是正確的,這裏是如何我順利地從顯示的圖像存儲在同一個JSON屬性的文本。
的document.getElementById(「weatherDescriptionData」 ).innerHTML = data.weather [0] .description;
這是HTML img標籤
<img src="" id="weatherIconData"></div>
這是我沒能成功,顯示圖像的第一個方法。 (向右滾動)
document.getElementById("weatherIconData").src = "http://openweathermap.org/img/w/" + data.weather[0].icon + ".png";
這是我嘗試顯示圖像失敗的第二種方法。
$("#weatherIconData").prop('src', "http://openweathermap.org/img/w/" + data.weather[0].icon + ".png");
我查了一下,發現
console.log(data.weather[0].icon);
確實事實上回報04D。
如何在HTML視圖中顯示JSON中提供的圖像?
@PencilCreate我編輯[@ user6101582的答案](http://stackoverflow.com/a/36236176/227299)添加一些更多的細節。被接受的答案可能有效,但它不是正確的解決方案,因爲它解決了一個並沒有真正理解的問題。沒有理由爲什麼應該在$ document.getElementById('id')' –