烦人的方块出现在天气应用程序中作为背景图片

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

嗨,我最近开始了一个 js 项目,称为天气应用程序。我制作这个项目是为了学习 fetch api,后来我添加了 css trsnsitions 和动画,无论如何,我完成了整个项目的功能和所有内容,但问题是看起来 Problem 正如你所看到的,我的程序员同事们有这个令人讨厌的方块主屏幕的东西我想知道如何删除它

Js代码:

const apiKey = "6997e66c1b3106660cb9cef841fd76f6";
const cityInp = document.getElementById("Cityinput");
const weatherImage = document.getElementById("weatherImage");

function Search() {
  const city = cityInp.value;
  fetch(
    `https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${apiKey}`
  )
    .then((resp) => resp.json())
    .then((data) => {
      const Weatherinfo = document.getElementById("weatherDetails");
      let weatherCondition = "";

      if (data.weather && data.weather.length > 0) {
        weatherCondition = data.weather[0].main;
      }
      const celsiusTemperature = parseInt(data.main.temp) - 273.15;
      const details = `<h2>${data.name}</h2>
            <p>${weatherCondition}</p>
            <p>Temperature: ${celsiusTemperature.toFixed(2)}°C</p>`;
      Weatherinfo.innerHTML = details;

      switch (weatherCondition) {
        case "Clear":
          weatherImage.src = "images/clear.png";
          break;
        case "Clouds":
          weatherImage.src = "images/cloud.png";
          break;
        case "Rain":
          weatherImage.src = "images/rain.png";
          break;
        case "Snow":
          weatherImage.src = "images/snow.png";
          break;
        case "Haze":
          weatherImage.src = "images/mist.png";
          break;
        default:
          weatherImage.src = "images/def.png";
      }
    });
}

HTML代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,[email protected],100..700,0..1,-50..200" />
    <link rel="stylesheet" href="style.css">
    <title>Weather App</title>
</head>
<body>
    <div class="Entry">
        <input type="text" id="Cityinput" placeholder="Enter a location">
        <button onclick="Search()"><span class="material-symbols-outlined">
            search
            </span></button>
        </div>
    <div id="weatherDetails">
    </div>
    <img src="" alt="" id="weatherImage">

    
</body>
<script src="Fetch.js"></script>
</html>

我一直在尝试删除正方形,我尝试过,将最后开关盒默认图像的背景更改为类似的背景图像,但没有发生任何事情,如果您回答,请尝试在回答时也进行解释,我是一个安静的初学者

javascript html css debugging background
1个回答
0
投票

那个“烦人的方块”就是

<img>
元素:

<img src="" alt="" id="weatherImage">

它没有显示图像,因为您希望它显示什么图像?

src
属性指定图像的来源。并且
""
没有指定任何内容。因此它成功显示了指定的(缺少的)图像。 (方框内的图标是浏览器的默认指示,表明页面正在尝试显示图像但失败。)

一种方法是显示某种“默认”图像。比如: <img src="" alt="images/def.png" id="weatherImage">

(或者您想要显示的任何默认图像值。)

另一个选项是设置要隐藏的元素的样式。例如,您可以添加 CSS 类来隐藏某些内容:

.hidden { display: none; }

并默认在元素中包含该类:

<img src="" alt="" id="weatherImage" class="hidden">

然后在 JavaScript 代码中,在显示图像时删除该类:

weatherImage.classList.remove("hidden");

如果您想重新隐藏该元素,只需再次添加该类即可:

weatherImage.classList.add("hidden");

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