如何在开放式天气地图上进行在线天气预报?

问题描述 投票:0回答:1

我已经准备好代码,但是它仅适用于第一个块。我需要每天的气温,白天和水温根据城市而​​变化。如何正确实施?我必须马上说代码的一部分是俄语,但是我想每个人都会理解我的:)请帮助我!

const api = {
	key: "11c85ec89d2ab590110511a07349dd25",
	baseurl: "https://api.openweathermap.org/data/2.5/"
}

const weather__wrapper = document.addEventListener('DOMContentLoaded', setQuery);
var check = document.querySelector('.weather__city').textContent;

function setQuery(evt) {
	getResults(check);
}


function getResults (query) {
	fetch(`${api.baseurl}weather?q=${query}&units=metric&APPID=${api.key}`)
	.then(weather=> {
		return weather.json();
	}).then(displayResults);
}


function displayResults(weather) {
	console.log(weather);
	let city = document.querySelectorAll('.weather__text .weather__city');
	city.innerText = `${weather.name}, ${weather.sys.country}`;

	let now = new Date();
	let date = document.querySelector('.weather__text .weather__date');
	date.innerText = dateBuilder(now);
}

function dateBuilder(d) {
	let months = ["01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"];

	let date = d.getDate();
	let month = months[d.getMonth()];
	let year = d.getFullYear();

 	return `${date}.${month}.${year}`;
}
.weather__wrapper {
	height: 186px;
	width: 355px;
	border-radius: 5px;
	background-color: #e4e8ef;
	padding: 24px 24px 24px 24px;


	margin-left: 30px; /* убрать */
}

.weather__icon {
	float: left;
	margin-top: 26px;
}

.weather__icon i {
	color: #fab165;
	font-size: 5.938em;
}

.weather__text {
	float: right;
}

.weather__city  { /* Локация, на видел input */
	font-weight: 400;
	font-size: 1.500em;
	margin-bottom: 0!important;
}

.weather__temperature {
	font-weight: 800;
	font-size: 1.500em;
	margin-bottom: 0!important;
}

.weather__date {
	font-size: 1.250em;
	font-weight: 200;
	margin-bottom: 0!important;
}

.slide-weather__items {
	display: flex;
	outline: none;
}

.slide-weather {
	margin-bottom: 100px;
}
<!DOCTYPE html>
<html lang="ru">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <link rel="stylesheet" href="css/main.css">

</head>
<body>
    <div class="slide-weather">
        Hello
        <div class="slide-weather__items">
            <div class="weather__wrapper">
                <!-- weather on video -->
                <div class="weather__icon"><i class="fas fa-sun"></i></div>
                <div class="weather__text">
                    <p class="weather__city sochi">Sochi</p>
                    <div class="weather__current-temp">
                        <p class="weather__temperature">+23<sup>o</sup>C - air</p>
                    </div>
                    <div class="weather__water">
                        <p class="weather__temperature">+23<sup>o</sup>C - water</p>    
                    </div>
                    <p class="weather__date">29.05.2020</p>
                </div>
            </div>
            <div class="weather__wrapper">
                <!-- weather on video -->
                <div class="weather__icon"><i class="fas fa-sun"></i></div>
                <div class="weather__text">
                    <p class="weather__city">Novorossiysk</p>
                    <div class="weather__current-temp">
                        <p class="weather__temperature">+23<sup>o</sup>C - air</p>
                    </div>
                    <div class="weather__water">
                        <p class="weather__temperature">+23<sup>o</sup>C - water</p>    
                    </div>
                    <p class="weather__date">29.05.2020</p>
                </div>
            </div>
            <div class="weather__wrapper">
                <!-- weather on video -->
                <div class="weather__icon"><i class="fas fa-sun"></i></div>
                <div class="weather__text">
                    <p class="weather__city">Gelendzhik</p>
                    <div class="weather__current-temp">
                        <p class="weather__temperature">+23<sup>o</sup>C - air</p>
                    </div>
                    <div class="weather__water">
                        <p class="weather__temperature">+23<sup>o</sup>C - water</p>    
                    </div>
                    <p class="weather__date">29.05.2020</p>
                </div>
            </div>
        </div>
    </div>


    <script src="js/jquery-3.5.1.min.js"></script>
    <script src="js/weather.js"></script>
</body>
</html>
javascript html openweathermap
1个回答
0
投票

您需要为每个元素运行该功能。类似于以下内容:

const wrappers = document.querySelectorAll('.weather__wrapper');

wrappers.forEach( w => {

   var city = w.querySelector('.weather__city');
   getResults(city);

});

但是您需要重构,以便每个块仅了解自身并在其内部打印数据。

© www.soinside.com 2019 - 2024. All rights reserved.