JS功能显示出搜索的结果,但是搜索其他内容时会消失

发布于 2025-02-14 01:18:50 字数 1225 浏览 0 评论 0 原文

婴儿开发人员在这里。因此,我正在使用Axios和Open Weather API创建一个天气应用程序,以获取实时预测信息。我在HTML中创建了一个空的DIV,然后创建了一个功能,该功能只有在搜索的城市具有有效的天气警报时,才能在页面底部显示警报。它效果很好,我唯一的问题是,一旦我搜索一个具有主动警报的城市,警报就一直留在那儿,除非我刷新页面,否则不会消失。
因此,例如,如果我搜索巴黎和巴黎有一个主动警报,则会出现,但是如果我搜索罗马和罗马没有主动警报,那么巴黎警报就一直留在那里,直到我刷新页面,

这是我的功能仅使用CSS HTML和JS

function showAlerts(response) {
  let alertsElement = document.querySelector("#alerts");
  if (response.data.alerts && response.data.alerts.length > 0) {
    alertsElement.style.backgroundColor = "#981f15";
    alertsHTML = `<div class="alert"> ⚠️ ALERT! ${response.data.alerts[0].event} </div>`;
    alertsElement.innerHTML = alertsHTML;
  } else if (response.data.alerts && response.data.alerts.length < 1) {
    alertsElement.innerHTML = "";
  }
}

这是指向我正在使用的API的链接,我的git存储库

https://github.com/sarahabousenna/vanilla/vanilla-weather--weather-weather-weather-application

Baby developer here. So, I’m creating a weather app using Axios and open weather API to grab live forecast info. I created an empty div in my HTML and then created a function that would show an alert at the bottom of the page ONLY if the city that was searched for has an active weather alert. It works great, the only issue I have is that once I search for a city that has an active alert the alert just stays there, does not disappear unless I refresh the page.
So for example if I search for Paris and paris has an active alert it appears but then if i search for rome and rome does not have an active alert, the paris alert just stays there until I refresh the page

Heres my function: I’m using css html and js only

function showAlerts(response) {
  let alertsElement = document.querySelector("#alerts");
  if (response.data.alerts && response.data.alerts.length > 0) {
    alertsElement.style.backgroundColor = "#981f15";
    alertsHTML = `<div class="alert"> ⚠️ ALERT! ${response.data.alerts[0].event} </div>`;
    alertsElement.innerHTML = alertsHTML;
  } else if (response.data.alerts && response.data.alerts.length < 1) {
    alertsElement.innerHTML = "";
  }
}

Heres a link to the API I’m using and my Git repository

https://openweathermap.org/api/one-call-3

https://github.com/SarahAbousenna/vanilla-weather-application

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

双马尾 2025-02-21 01:18:50

谢谢大家的回答,他们确实提供了帮助!

我还尝试了一些也有效的东西,

我将功能更新为&gt;&gt;

function showAlerts(response) {
  let alertsElement = document.querySelector("#alerts");
  if (response.data.alerts && response.data.alerts.length > 0) {
    alertsElement.style.backgroundColor = "#981f15";
    alertsHTML = `<div class="alert"> ⚠️ ALERT! ${response.data.alerts[0].event} </div>`;
    alertsElement.innerHTML = alertsHTML;
  } else {
    alertsElement.innerHTML = "";
    alertsElement.style.backgroundColor = "#fdf8f4";
  }
}

Thank you all for your answers they really helped!

I have also tried something that also worked

I updated the function to >>

function showAlerts(response) {
  let alertsElement = document.querySelector("#alerts");
  if (response.data.alerts && response.data.alerts.length > 0) {
    alertsElement.style.backgroundColor = "#981f15";
    alertsHTML = `<div class="alert"> ⚠️ ALERT! ${response.data.alerts[0].event} </div>`;
    alertsElement.innerHTML = alertsHTML;
  } else {
    alertsElement.innerHTML = "";
    alertsElement.style.backgroundColor = "#fdf8f4";
  }
}

掐死时间 2025-02-21 01:18:50

response.data.Alerts 将是不确定的,或者在没有警报的情况下为空。在第二个条件下,您将检查有效的 response.data.alerts 字段,这不是正确的条件。相反,以下片段可用于清除 div

function showAlerts(response) {
  let alertsElement = document.querySelector("#alerts");
  if (response.data.alerts && response.data.alerts.length > 0) {
    alertsElement.style.backgroundColor = "#981f15";
    alertsHTML = `<div class="alert"> ⚠️ ALERT! ${response.data.alerts[0].event} </div>`;
    alertsElement.innerHTML = alertsHTML;
  }
  if (!response.data.alerts || response.data.alerts.length === 0) {
    alertsElement.style.backgroundColor = "#000000";
    alertsElement.innerHTML = '';
  }
}

response.data.alerts would be undefined or empty in case there are no alerts. Under the second if condition, you are checking for a valid response.data.alerts field, which is not the correct condition. Instead, the following snippet can be used to clear out the div:

function showAlerts(response) {
  let alertsElement = document.querySelector("#alerts");
  if (response.data.alerts && response.data.alerts.length > 0) {
    alertsElement.style.backgroundColor = "#981f15";
    alertsHTML = `<div class="alert"> ⚠️ ALERT! ${response.data.alerts[0].event} </div>`;
    alertsElement.innerHTML = alertsHTML;
  }
  if (!response.data.alerts || response.data.alerts.length === 0) {
    alertsElement.style.backgroundColor = "#000000";
    alertsElement.innerHTML = '';
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文