[英]How to reset CSS when performing a second search (remove search result from 1st search)
I'm building a simple weather app in ES6 that is supposed to return a song from Spotify that fits the current weather alongside with the forecast. 我正在ES6中构建一个简单的气象应用程序,该应用程序应该从Spotify返回一首适合当前天气和天气预报的歌曲。 However, when a user performs two searches in a row for different locations it will still show the song from the first search.
但是,当用户连续两次针对不同的位置执行搜索时,仍会显示第一次搜索中的歌曲。
Currently, I'm hiding the song tracks in CSS with display: none and changing the display to block in JS depending on the search result through if-statements. 当前,我用显示隐藏CSS中的歌曲轨道:无,然后通过if语句根据搜索结果将显示更改为在JS中阻止。 How can I remove the first song, so from the first search?
如何从第一首搜索中删除第一首歌曲?
I tried changing the if-statements to a switch statement but it stopped displaying the songs altogether. 我尝试将if语句更改为switch语句,但是它完全停止显示歌曲。 Another option I thought of was using classList but I'm not sure how...
我想到的另一个选择是使用classList,但我不确定如何...
CSS CSS
#musicRain,
#musicClear,
#musicThunderstorm,
#musicSnow,
#musicClouds,
#musicAtmosphere {
display: none;
}
JS JS
function showCurrentWeather(response) {
let currentTemperature = document.querySelector("#currentTemperature");
currentTemperature.innerHTML = `${Math.round(response.data.main.temp)}°C`;
[etc... ]
if (response.data.weather[0].main === "Clear") {
document.getElementById("musicClear").style.display = "block";
}
if (response.data.weather[0].main === "Drizzle") {
document.getElementById("musicDrizzle").style.display = "block";
}
if (response.data.weather[0].main === "Rain") {
document.getElementById("musicRain").style.display = "block";
}
if (response.data.weather[0].main === "Clouds") {
document.getElementById("musicClouds").style.display = "block";
} else {
document.getElementById("musicAtmosphere").style.display = "block";
}
}
You want to set every element's display
field to either block
or none
so that earlier settings won't linger. 您希望将每个元素的
display
字段设置为“ block
或“ none
这样就不会留下较早的设置。 Since they all start with "music" you could do this: 由于它们都是以“音乐”开头的,因此您可以这样做:
const types = ["Clear","Drizzle","Rain","Clouds","Atmosphere"];
types.forEach( type => {
const musicType = `music${type}`
if (response.data.weather[0].main === type) {
document.getElementById(musicType).style.display = "block";
}
else {
document.getElementById(musicType).style.display = "none";
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.