我有基本的 if-else 陳述句。它無法讀取 if 部分之后的 else if 和 else。它只讀取第一如果。這是我的代碼。這里可能有什么問題。
if (screen.width < 992) {
alert('Less than 992');
$("#res_img").toggleClass('col-lg-6 col-lg-5');
$("#res_img_two").toggleClass('col-lg-6 col-lg-7');
} else if (screen.width < 425) {
alert('Less than 425');
$("#res_img").toggleClass('col-5 col-12');
$("#res_img_two").toggleClass('col-7 col-12');
}
else {
console.log('success');
}
uj5u.com熱心網友回復:
首先,您的代碼在每次重新加載頁面時只會運行一次,請在測驗中注意這一點。其次,讓我們用更簡單的“偽”指令重寫您在代碼中撰寫的內容:
if width of the screen is less than 2 pixels {
say 'Less than 2';
} else if width of the screen is less than 1 pixel {
say 'Less than 1';
}
else {
say 'success';
}
也許這樣更清楚,每個小于 1 的螢屏總是小于 2 個像素,所以,當你這樣問時,真的沒有真正的方法來區分這兩種情況。我們可以嘗試像這樣重新排列這個問題:
if width of the screen is less than 1 pixels {
say 'Less than 1';
} else if width of the screen is less than 2 pixel {
say 'Less than 2';
}
else {
say 'success';
}
那么每一個小于 1 的螢屏總是小于 2 個像素,如果大于 1 但小于 2,那么只有第二個陳述句為真。但我認為我們的意圖可以更清楚地寫成:
if width of the screen is less than 2 pixels and width of the screen is more than 1 {
say 'Less than 2 but more than 1';
} else if width of the screen is less than 1 pixel {
say 'Less than 1';
}
else {
say 'success';
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/343463.html
標籤:javascript 查询
上一篇:實作導航欄選單的切換狀態
