【问题标题】:Struggle calling HTML classes in a Javascript for loop努力在 Javascript for 循环中调用 HTML 类
【发布时间】:2018-03-29 04:33:33
【问题描述】:

我正在尝试为学校建立一个天气网站。我已经完成了根据用户当前位置准确显示天气信息的主页。我现在正试图预测未来 5 天。我可以成功构建它,但代码很长,因为我没有使用循环。但是在尝试构建循环时,我无法让它工作。

这是我的代码:

$(function () {
  $.simpleWeather({
    location: 'Boston, MA',
    unit: 'c',
    success: function (weather) {
      /*day1 = weather.forecast[1].day;
      image1 = '<img class="weathericon" src="img/weathericons/' + weather.forecast[1].code + '.svg" alt="weather icon">';
      temp1 = weather.forecast[1].high + '&deg;';
      text1 = weather.forecast[1].text;

      day2 = weather.forecast[2].day;
      image2 = '<img class="weathericon" src="img/weathericons/' + weather.forecast[2].code + '.svg" alt="weather icon">';
      temp2 = weather.forecast[2].high + '&deg;';
      text2 = weather.forecast[2].text;

      day3 = weather.forecast[3].day;
      image3 = '<img class="weathericon" src="img/weathericons/' + weather.forecast[3].code + '.svg" alt="weather icon">';
      temp3 = weather.forecast[3].high + '&deg;';
      text3 = weather.forecast[3].text;

      day4 = weather.forecast[4].day;
      image4 = '<img class="weathericon" src="img/weathericons/' + weather.forecast[4].code + '.svg" alt="weather icon">';
      temp4 = weather.forecast[4].high + '&deg;';
      text4 = weather.forecast[4].text;

      day5 = weather.forecast[5].day;
      image5 = '<img class="weathericon" src="img/weathericons/' + weather.forecast[5].code + '.svg" alt="weather icon">';
      temp5 = weather.forecast[5].high + '&deg;';
      text5 = weather.forecast[5].text;

      $(".day1").html(day1);
      $(".image1").html(image1);
      $(".temp1").html(temp1);
      $(".text1").html(text1);

      $(".day2").html(day2);
      $(".image2").html(image2);
      $(".temp2").html(temp2);
      $(".text2").html(text2);

      $(".day3").html(day3);
      $(".image3").html(image3);
      $(".temp3").html(temp3);
      $(".text3").html(text3);

      $(".day4").html(day4);
      $(".image4").html(image4);
      $(".temp4").html(temp4);
      $(".text4").html(text4);

      $(".day5").html(day5);
      $(".image5").html(image5);
      $(".temp5").html(temp5);
      $(".text5").html(text5);*/

      for(var i=0;i<weather.forecast.length;i++){
        day[i] = weather.forecast[i].day;
        image[i] = '<img class="weathericon" src="img/weathericons/' + weather.forecast[i].code + '.svg" alt="weather icon">';
        temp[i] = weather.forecast[i].high + '&deg;';
        text[i] = weather.forecast[i].text;      

        $(".day"+i).html(day[i]);
        $(".image"+i).html(image[i]);
        $(".temp"+i).html(temp[i]);
        $(".text"+i).html(text[i]);
      }
    },
    error: function (error) {
      $("#weather").html('<p>' + error + '</p>');
    }
  });
});

https://codepen.io/Evexium/pen/wrQOqb

只是for循环:

for(var i=0;i<weather.forecast.length;i++){
        day[i] = weather.forecast[i].day;
        image[i] = '<img class="weathericon" src="img/weathericons/' + weather.forecast[i].code + '.svg" alt="weather icon">';
        temp[i] = weather.forecast[i].high + '&deg;';
        text[i] = weather.forecast[i].text;      

        $(".day"+i).html(day[i]);
        $(".image"+i).html(image[i]);
        $(".temp"+i).html(temp[i]);
        $(".text"+i).html(text[i]);
      }

我想我的问题在这里:

$(".day"+[i]).html(day[i]);
$(".image"+[i]).html(image[i]);
$(".temp"+[i]).html(temp[i]);
$(".text"+[i]).html(text[i]);

但我不知道如何使它工作。帮助会很棒!

【问题讨论】:

    标签: javascript html weather


    【解决方案1】:

    这些行看起来不对:

    $(".day[i]").html(day[i]);
    $(".image"+[i]).html(image[i]);
    

    正确的 JQuery 选择器应该如下所示:

    $(".day"+i).html(day[i]);
    $(".image"+i).html(image[i]);
    

    假设你的班级是day1、image1等

    【讨论】:

    • 是的,这些是我的课程,出于某种原因,我在 Chrome 的控制台中收到错误消息:ReferenceError: day is not defined。此错误是 forloop 内的第一天。
    • 您需要使用var 关键字定义您的变量。
    猜你喜欢
    • 2021-02-25
    • 2014-12-01
    • 2023-03-18
    • 1970-01-01
    • 2021-08-15
    • 1970-01-01
    • 1970-01-01
    • 2015-02-24
    • 1970-01-01
    相关资源
    最近更新 更多