婴儿开发人员在这里。因此,我正在使用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
发布评论
评论(2)
谢谢大家的回答,他们确实提供了帮助!
我还尝试了一些也有效的东西,
我将功能更新为&gt;&gt;
Thank you all for your answers they really helped!
I have also tried something that also worked
I updated the function to >>
response.data.Alerts
将是不确定的
,或者在没有警报的情况下为空。在第二个条件下,您将检查有效的response.data.alerts
字段,这不是正确的条件。相反,以下片段可用于清除div
:response.data.alerts
would beundefined
or empty in case there are no alerts. Under the second if condition, you are checking for a validresponse.data.alerts
field, which is not the correct condition. Instead, the following snippet can be used to clear out thediv
: