嵌套的 if else 语句仅适用于第一条语句
Nested if else statements only works on first statement
我对嵌套的 if else 语句有疑问。在这个问题中,它只执行第一个 if 语句并跳过所有其余的,当我想要执行每个语句时。 else if 语句在其中没有嵌套任何内容时工作正常,但是当我嵌套其他 if else 语句时,只有第一个似乎工作。这是我的 javascript 代码:
const apiURL2 = "https://api.openweathermap.org/data/2.5/forecast?id=5604473&appid=6d1d830097a2c0bac1aba2337d0139e6";
fetch(apiURL2).then((response) => response.json()).then((jsonObject) => {
const list = jsonObject['list'];
console.log(jsonObject);
for ( let i = 0; i < 5; i++){
let divForecastData = document.querySelector('div.weather-forecast-wrapper');
let temp1 = document.createElement("div");
let tempday = document.createElement("p");
let temperature = document.createElement("p");
let icon = document.createElement("i");
temperature.className = "temp";
tempday.className = "weekday";
temp1.className = "day";
if (i == 0){
tempday.textContent = "Sat";
temperature.textContent = list[i].main.temp;
if (list[i].weather[i].main = "Clear"){
icon.className = "worked"
}
else {
icon.className = " still worked"
}
}
else if (i == 1){
tempday.textContent = "Sun";
var far = list[i].main.temp
var kel = far * (9/5) - 459.67;
temperature.textContent = Math.round(kel) + "℉";
if (list[i].weather[i].main = "Clear"){
icon.className = "worked"
}
else {
icon.className = " still worked"
}
}
else if (i == 2){
tempday.textContent = "Mon";
temperature.textContent = list[i].main.temp;
}
else if (i == 3){
tempday.textContent = "Wed";
temperature.textContent = list[i].main.temp;
}
else{
tempday.textContent = "Thu";
temperature.textContent = list[i].main.temp;
}
divForecastData.appendChild(temp1);
temp1.appendChild(tempday);
temp1.appendChild(icon);
temp1.appendChild(temperature);
}
});
有什么建议吗?
比较用 ==
或 ===
...
编码
所以这样不好。
if (list[i].weather[i].main = "Clear")
但是 list[i].weather[i]
有些东西不存在,我知道是因为 运行 代码会在控制台上显示一条红色的大消息。您可能想使用 list[i].weather[0]
.
这是更正后的代码片段
const apiURL2 = "https://api.openweathermap.org/data/2.5/forecast?id=5604473&appid=6d1d830097a2c0bac1aba2337d0139e6";
fetch(apiURL2).then((response) => response.json())
.then((jsonObject) => {
let divForecastData = document.querySelector('div.weather-forecast-wrapper');
console.log(jsonObject);
const list = jsonObject['list'];
for ( let i = 0; i < 5; i++){
const item = list[i];
let temp1 = document.createElement("div");
let tempday = document.createElement("p");
let temperature = document.createElement("p");
let icon = document.createElement("i");
temperature.className = "temp";
tempday.className = "weekday";
temp1.className = "day";
if (i == 0){
tempday.textContent = "Sat";
temperature.textContent = item.main.temp;
if (item.weather[i].main = "Clear"){
icon.className = "worked"
} else {
icon.className = " still worked"
}
} else if (i == 1){
tempday.textContent = "Sun";
var far = item.main.temp
var kel = far * (9/5) - 459.67;
temperature.textContent = Math.round(kel) + "℉";
if (item.weather[0].main === "Clear"){
icon.className = "worked"
} else {
icon.className = " still worked"
}
} else if (i == 2){
tempday.textContent = "Mon";
temperature.textContent = item.main.temp;
} else if (i == 3){
tempday.textContent = "Wed";
temperature.textContent = item.main.temp;
} else{
tempday.textContent = "Thu";
temperature.textContent = item.main.temp;
}
divForecastData.appendChild(temp1);
temp1.appendChild(tempday);
temp1.appendChild(icon);
temp1.appendChild(temperature);
}
});
<div class='weather-forecast-wrapper'></div>
我对嵌套的 if else 语句有疑问。在这个问题中,它只执行第一个 if 语句并跳过所有其余的,当我想要执行每个语句时。 else if 语句在其中没有嵌套任何内容时工作正常,但是当我嵌套其他 if else 语句时,只有第一个似乎工作。这是我的 javascript 代码:
const apiURL2 = "https://api.openweathermap.org/data/2.5/forecast?id=5604473&appid=6d1d830097a2c0bac1aba2337d0139e6";
fetch(apiURL2).then((response) => response.json()).then((jsonObject) => {
const list = jsonObject['list'];
console.log(jsonObject);
for ( let i = 0; i < 5; i++){
let divForecastData = document.querySelector('div.weather-forecast-wrapper');
let temp1 = document.createElement("div");
let tempday = document.createElement("p");
let temperature = document.createElement("p");
let icon = document.createElement("i");
temperature.className = "temp";
tempday.className = "weekday";
temp1.className = "day";
if (i == 0){
tempday.textContent = "Sat";
temperature.textContent = list[i].main.temp;
if (list[i].weather[i].main = "Clear"){
icon.className = "worked"
}
else {
icon.className = " still worked"
}
}
else if (i == 1){
tempday.textContent = "Sun";
var far = list[i].main.temp
var kel = far * (9/5) - 459.67;
temperature.textContent = Math.round(kel) + "℉";
if (list[i].weather[i].main = "Clear"){
icon.className = "worked"
}
else {
icon.className = " still worked"
}
}
else if (i == 2){
tempday.textContent = "Mon";
temperature.textContent = list[i].main.temp;
}
else if (i == 3){
tempday.textContent = "Wed";
temperature.textContent = list[i].main.temp;
}
else{
tempday.textContent = "Thu";
temperature.textContent = list[i].main.temp;
}
divForecastData.appendChild(temp1);
temp1.appendChild(tempday);
temp1.appendChild(icon);
temp1.appendChild(temperature);
}
});
有什么建议吗?
比较用 ==
或 ===
...
所以这样不好。
if (list[i].weather[i].main = "Clear")
但是 list[i].weather[i]
有些东西不存在,我知道是因为 运行 代码会在控制台上显示一条红色的大消息。您可能想使用 list[i].weather[0]
.
这是更正后的代码片段
const apiURL2 = "https://api.openweathermap.org/data/2.5/forecast?id=5604473&appid=6d1d830097a2c0bac1aba2337d0139e6";
fetch(apiURL2).then((response) => response.json())
.then((jsonObject) => {
let divForecastData = document.querySelector('div.weather-forecast-wrapper');
console.log(jsonObject);
const list = jsonObject['list'];
for ( let i = 0; i < 5; i++){
const item = list[i];
let temp1 = document.createElement("div");
let tempday = document.createElement("p");
let temperature = document.createElement("p");
let icon = document.createElement("i");
temperature.className = "temp";
tempday.className = "weekday";
temp1.className = "day";
if (i == 0){
tempday.textContent = "Sat";
temperature.textContent = item.main.temp;
if (item.weather[i].main = "Clear"){
icon.className = "worked"
} else {
icon.className = " still worked"
}
} else if (i == 1){
tempday.textContent = "Sun";
var far = item.main.temp
var kel = far * (9/5) - 459.67;
temperature.textContent = Math.round(kel) + "℉";
if (item.weather[0].main === "Clear"){
icon.className = "worked"
} else {
icon.className = " still worked"
}
} else if (i == 2){
tempday.textContent = "Mon";
temperature.textContent = item.main.temp;
} else if (i == 3){
tempday.textContent = "Wed";
temperature.textContent = item.main.temp;
} else{
tempday.textContent = "Thu";
temperature.textContent = item.main.temp;
}
divForecastData.appendChild(temp1);
temp1.appendChild(tempday);
temp1.appendChild(icon);
temp1.appendChild(temperature);
}
});
<div class='weather-forecast-wrapper'></div>