简体   繁体   English

在执行第二次搜索时如何重置CSS(从第一次搜索中删除搜索结果)

[英]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.

相关问题 从列表中搜索并在找到搜索结果时删除兄弟姐妹 - Search from a list and remove siblings when search result found 如何搜索表的第二列而不是第一列 - How to search 2nd column of table instead of 1st column 如何在JavaScript集合中搜索多个词组之间的第一次匹配? - How to Search a JavaScript Collection for the 1st Occurrence Between Multiple Phrases? 如何搜索字符串中第一次出现的“:/”,然后搜索找到的子字符串中所有其他出现的内容,包括“:/”? - How to search a string for 1st occurrence of “:/” and then search all other occurences of the found substring inclusive “:/”? 如何/何时删除子元素以清除搜索结果? - How/When to remove child elements to clear search result? 搜索完成后从搜索结果中删除/隐藏标记 -Leaflet - Remove /Hide Marker from search result after search is finished -Leaflet 在执行搜索之前,如何使我的实时jQuery搜索等待一秒钟? - How do I make my live jQuery search wait a second before performing the search? 没有搜索结果时从div隐藏div - hide div from div when there is no search result 如何从Google Web搜索api重定向搜索结果 - how to redirect the search result from google web search api 当我单击搜索进行第二次搜索时,先前的搜索将被删除 - Previous search to be removed when I click search for a second search
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM