所以,我正在製作這個應用程序。我基本上想要做的是顯示當前的溫度,然後顯示穿着大衣的人的圖像或沒有穿大衣的人的圖像。 我已經有當前的臨時顯示,(我正在使用該值的API)但我無法弄清楚如何做圖像的一部分。 我使用HTML和Javascript(使用JQuery和JSON與API一起使用)。 我知道我可能。需要使用if語句,但我不知道如何在JavaScript中使用圖像。 我將temp插入表中,我希望圖像與表中的temp相同。如何顯示某些值的某些圖像
的JavaScript和HTML(這是很基本的)低於
(function() {
\t "use strict";
\t
\t var main = function() {
\t \t var url = 'http://api.worldweatheronline.com/premium/v1/weather.ashx?key=MY_PRIVATE_KEY&num_of_days=2&format=JSON&callback=?';
\t \t $.getJSON(url, function(weatherResponse) {
\t \t \t console.log(weatherResponse);
\t \t \t var $weatherTable = $('<table>');
\t \t \t var response = weatherResponse.data.current_condition[0];
\t \t \t var count = 0;
for (var prop in response) {
if (count == 16) {
\t \t \t \t var $item = $('<tr>');
\t \t \t \t var $itemProp = $('<td>').text(prop);
\t \t \t \t var $itemVal = $('<td>').text(response[prop]);
\t \t \t \t $item.append("Temperature feels like: ");
\t \t \t \t $item.append($itemVal);
\t \t \t \t $weatherTable.append($item);
}
count = count+1;
}
\t \t \t
\t \t \t $('main').append($weatherTable);
\t \t });
\t };
\t
\t $(document).ready(main);
}());
<html>
\t <head>
\t \t <title>Weather</title>
\t </head>
\t <body>
\t \t
\t \t <header>
\t \t \t <h1>Weather</h1>
\t \t </header>
\t \t
\t \t <main>
\t \t \t \t \t \t
\t \t </main>
\t \t
\t \t <footer>
\t \t \t <p>Made by </p>
\t \t </footer>
\t \t
\t \t <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
\t \t <script src='sun.js'></script>
\t </body>
</html>
對不起,我排除了我的API密鑰(因爲它是私有的,我不得不創建一個帳戶,以獲得一個。)因此,你不能運行代碼 – ineedtoknow
你能不能從推斷我的例子?關於hwo添加圖片並設置src標籤的任何問題? – Neil