【发布时间】:2020-10-03 22:22:45
【问题描述】:
我有一个循环,它为接下来的 7 天中的每一天创建一个元素,并显示特定日期的天气数据。 (从 API 获取数据)。我不知道如何在每个循环中插入当天的日期。日期应在span 元素中,id 为“daily-weather”
我想要实现的日期格式是: “太阳” “1 月 31 日”
dataWeakly.daily.forEach((day, index) => {
const uvIndex: string = Math.floor(+day.uvi).toString();
if (index < 1) {
return;
}
const markup = `
<div class="weather-daily">
<span id="daily-date"></span>
<span id="daily-date"></span>
<img id="daily-img" src=http://openweathermap.org/img/wn/${
day.weather[0].icon
}@2x.png alt="weather img"></img>
<span>${toTitleCase(day.weather[0].description)}</span>
<span id="max-weather">${formatNumber(
day.temp.max
)}°c\u00A0\u00A0</span><span id="min-weather">\u00A0${formatNumber(
day.temp.min
)}°c</span>
<span>UV:\u00A0</span> <span id="uv-index">${uvIndex}</span>
</div>
`;
document
.getElementById("weekly-result-container")
.insertAdjacentHTML("afterbegin", markup);
【问题讨论】:
-
恐怕现代浏览器无法做到这一点,因为它们可以防止跨站点脚本。
-
使用momentjs处理天数和日期显示,比原生Date对象更加灵活方便。
-
谢谢@Frax 会检查一下 momentjs
标签: javascript html loops date foreach