假设以下json字符串为气象局提供的实时天气,请根据以下weather属性,结合 图片文件夹下的图片,将天气信息显示在html页面中 在页面中显示城市:xx 温度:xxx 天气:xxxx 并配上相应图片。 let jsonObj1 = `[{"temp":"20℃/30℃","weather":"多云转晴","name":"上海","pm":"80","wind":"1级"}, {"temp":"15℃/24℃","weather":"天晴","name":"北京","pm":"98","wind":"3级"}, {"temp":"26℃/32℃","weather":"多云转阴","name":"广州","pm":"30","wind":"2级"}]`;
时间: 2023-07-17 21:58:22 浏览: 164
网络天气获取JSON数据
5星 · 资源好评率100%
你可以使用以下代码将天气信息显示在HTML页面中,并配上相应的图片:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>天气信息</title>
</head>
<body>
<div id="weather-info"></div>
<script>
let jsonObj1 = JSON.parse(`[{"temp":"20℃/30℃","weather":"多云转晴","name":"上海","pm":"80","wind":"1级"},
{"temp":"15℃/24℃","weather":"天晴","name":"北京","pm":"98","wind":"3级"},
{"temp":"26℃/32℃","weather":"多云转阴","name":"广州","pm":"30","wind":"2级"}]`);
for (let i = 0; i < jsonObj1.length; i++) {
let city = jsonObj1[i].name;
let temperature = jsonObj1[i].temp;
let weather = jsonObj1[i].weather;
let imgSrc = ""; // 用于存储图片路径
if (weather === "多云转晴") {
imgSrc = "图片文件夹下的多云转晴图片路径";
} else if (weather === "天晴") {
imgSrc = "图片文件夹下的天晴图片路径";
} else if (weather === "多云转阴") {
imgSrc = "图片文件夹下的多云转阴图片路径";
}
let weatherInfo = document.createElement("p");
weatherInfo.innerHTML = `城市:${city} 温度:${temperature} 天气:${weather}`;
document.getElementById("weather-info").appendChild(weatherInfo);
let weatherImg = document.createElement("img");
weatherImg.src = imgSrc;
document.getElementById("weather-info").appendChild(weatherImg);
}
</script>
</body>
</html>
```
请将代码中的`图片文件夹下的多云转晴图片路径`、`图片文件夹下的天晴图片路径`和`图片文件夹下的多云转阴图片路径`替换为你实际存储图片的路径。
阅读全文