HTML
JavaScript if, else, and else if if (condition 狀況) { block of code to be executed if the condition is true 條件為真時執行的代碼塊 } else if (condition2 狀況) { block of code to be executed if the condition1 is false and condition2 is true 條件 1 為假且條件 2 為真時執行的代碼塊 } else { block of code to be executed if the condition is false 條件為假時執行的代碼塊 } 比較運算子: 等於: == 不等於:!== 大於: > 小於:< 大於等於: >= 小於等於:<= 連接兩個以上的條件都是true && 連接兩個其中一個條件成true ||
script if 條件敘述 <input onclick="Fif(this.value)" type="button" value="10" /> <input onclick="Fif(this.value)" type="button" value="30" /> <input onclick="Fif(this.value)" type="button" value="60" /> <input onclick="Fif(this.value)" type="button" value="90" /> <input onclick="Fif(this.value)" type="button" value=" " /> <span id="txt">??</span> <script> var Select = "??" ; if(x==10){ Select = "ten"} if(x==30){ Select = "thirty"} if(x==60){ Select = "sixty"} if(x==90){ Select = "ninety"} txt.innerHTML =Select ; } </script> ??
script if 條件敘述 <input onclick="Fif(this.value)" type="button" value="10" /> <input onclick="Fif(this.value)" type="button" value="30" /> <input onclick="Fif(this.value)" type="button" value="60" /> <input onclick="Fif(this.value)" type="button" value="90" /> <input onclick="Fif(this.value)" type="button" value=" " /> <span id="txt">??</span> <script> function Fif(x) { var Select = (x==10)?"is 10" : (x==30)?"is 30" : (x==60)?"is 60": (x>10)?"is 90": "??"; txt.innerHTML = Select ; } </script> ??
script if 條件敘述 請選擇 30 Please select 30 <input onclick="Fif(this.value)" type="button" value="10" /> <input onclick="Fif(this.value)" type="button" value="30" /> <input onclick="Fif(this.value)" type="button" value="60" /> <input onclick="Fif(this.value)" type="button" value="90" /> <input onclick="Fif(this.value)" type="button" value=" " /> <span id="txt">??</span> <script> function Fif(x) { if (x==30){txt.innerHTML= '選擇正確choose correctly';return } else if( x < 30) {txt.innerHTML='選擇小於/ hoose less than 30'} else {txt.innerHTML='選擇大於/ elect greater than 30' } } </script> 請選擇 30 Please select 30 ??