篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了html 基本IF,IF - ELSE和IF - ELSE IF - ELSE语句。相关的知识,希望对你有一定的参考价值。
<!-- Basic IF statement will have an IF statement (what to do if the test is true), and an ELSE statement (what to do if the test is not true). You can have multiple logical tests by using an IF statement, any number of ELSE IF statements, and an ELSE statment in case none of the IF or ELSE IF statements are true. -->
<script language="javascript" type="text/javascript">
var age = 18;
if (age <= 10)
{
document.write("You can't vote, you're not even a teenager<br/>");
}
else if (age < 18)
{
document.write("You can't vote, but you're getting closer to being old enough<br/>");
}
else
{
document.write("You CAN vote, yay!<br/>");
}
/*
Comparision Operators
== Equivlency
=== Equivlent in value and type
1 == "1" true
1 === "1" false
> Greater Than
< Less Than
>= GT or =
<= LT or =
!= Not Equal To
&& And (used to combine two logical operators)
|| Or (used to create an OR statment between two logical operators
*/
</script>
<!-- DETAIL!!! -->
<script language="javascript" type="text/javascript">
var age = 18; // Creates variable age, sets it to a value of 18
if (age <= 10) // First logical test, IF statement, if age variable is less than or equal to 10,
{
document.write("You can't vote, you're not even a teenager<br/>"); // Then write 'You can't vote, you're not even a teenager'
}
else if (age < 18) // Second logical test, ELSE IF statement (you can have as many of these as you want), they are tested in order, if age viarable is less than 18 (but over 10 due to the fact that 10 and under was already tested in the first IF statement),
{
document.write("You can't vote, but you're getting closer to being old enough<br/>"); // Then write 'You can't vote, but you're getting closer to being old enough'
}
else // ELSE, what to do if none of the previous logical tests are true,
{
document.write("You CAN vote, yay!<br/>"); // Then write, 'You CAN vote, yay!'
}
</script>
以上是关于html 基本IF,IF - ELSE和IF - ELSE IF - ELSE语句。的主要内容,如果未能解决你的问题,请参考以下文章