如何通过Javascript向div添加图标

时间:2017-06-12 16:37:40

标签: javascript api icons add weather

我没什么问题。我不知道如何将图标插入div。我从天气API获取数据,我想创建我的天气小部件。这是我的代码的一部分:

  data.list.filter((value) => value.dt_txt.slice(0, 10) == date.toISOString().slice(0, 10) ? value : null).forEach(function(obj) {

          date_elements[i] += `<tr>
    <td>
    date: ${obj.dt_txt} </br>
    wind speed: ${obj.wind.speed}</br>
    pressure: ${obj.main.pressure}</br>
    temperature: ${obj.main.temp}</br>
    humidity: ${obj.main.humidity}</br>
    icon: ${obj.weather.icon}
    </td>
  </tr>`

        });

如果我插入这样的代码:

console.log(data.list[0].weather[0].icon);

我看到了我的Icon的属性,但我不知道如何添加url图标。

1 个答案:

答案 0 :(得分:0)

I'm not really sure about your question but if your data.list[0].weather[0].icon is a URL string to the icon you want to append to the table row, why don't you try this:
change your the line before your </td> tag to icon: <img src = "${data.list[0].weather[0].icon}">