⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 js_if_else.asp@output=print

📁 W3Schools tutorial..web designing
💻 ASP@OUTPUT=PRINT
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en-US" xml:lang="en-US" xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>JavaScript If...Else Statements</title>
 
<link rel="shortcut icon" href="../favicon.ico" type="image/x-icon" />
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<meta name="Keywords" content="xml,tutorial,html,dhtml,css,xsl,xhtml,javascript,asp,ado,vbscript,dom,sql,colors,soap,php,authoring,programming,training,learning,beginner's guide,primer,lessons,school,howto,reference,examples,samples,source code,tags,demos,tips,links,FAQ,tag list,forms,frames,color table,w3c,cascading style sheets,active server pages,dynamic html,internet,database,development,Web building,Webmaster,html guide" />

<meta name="Description" content="Free HTML XHTML CSS JavaScript DHTML XML DOM XSL XSLT RSS AJAX ASP ADO PHP SQL tutorials, references, examples for web building." />

<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="cache-control" content="no-cache" />

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "../../https@ssl./default.htm" : "../../www./default.htm");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-3855518-1");
pageTracker._initData();
pageTracker._trackPageview();
</script>

</head>
<body>

<p>From <b>http://www.w3schools.com</b> (Copyright Refsnes Data)</p>

<h1>JavaScript If...Else Statements</h1>
<a href="js_comparisons.asp"><img alt="previous" border="0" src="../images/btn_previous.gif" width="100" height="20" /></a>
<a href="js_switch.asp"><img alt="next" border="0" src="../images/btn_next.gif" width="100" height="20" /></a>
<hr />
<p class="intro">Conditional statements in JavaScript are used to perform different actions based on different conditions.</p>
<hr />

<h2>Examples</h2>
<p><a target="_blank" href="tryit.asp@filename=tryjs_ifthen">If statement</a><br />
How to write an if statement.</p>

<p><a target="_blank" href="tryit.asp@filename=tryjs_ifthenelse">If...else statement</a><br />
How to write an if...else statement.</p>

<p><a target="_blank" href="tryit.asp@filename=tryjs_elseif">If..else if...else statement</a><br />
How to write an if..else if...else statement.</p>

<p><a target="_blank" href="tryit.asp@filename=tryjs_randomlink">Random link</a><br />
This example demonstrates a link, when you click on the link it will take
you to W3Schools.com OR to RefsnesData.no. There is a 50% chance for each of
them.</p>

<hr />

<h2>Conditional Statements</h2>
<p>Very often when you write code, you want to perform different actions for
different decisions. You can use conditional statements in your code to do this.</p>
<p>In JavaScript we have the following conditional statements:</p>
<ul>
  <li><b>if statement</b> - use this statement if you want to execute some
    code only if a specified condition is true</li>
  <li><b>if...else statement</b> - use this statement if you want to execute some code if 
	the condition is true and another code if the condition is false</li>
	<li><b>if...else if....else statement</b>  - use this statement if you want 
	to select one of many blocks of code to be executed</li>
  <li><b>switch statement</b> - use this statement if you want to select one of 
	many blocks of code to be executed</li>
</ul>
<hr />

<h2>If Statement</h2>
<p>You should use the if statement if you want to execute some code only if a
specified condition is true.</p>
<h3>Syntax</h3>
<table class="ex" cellspacing="0" border="1" width="100%">
<tr>
<td>
<pre>if (<i>condition</i>)
{
<i>code to be executed if condition is true</i>
}</pre>
</td>
</tr>
</table>
<p>Note that if is written in lowercase letters. Using uppercase letters (IF) will 
generate a JavaScript error!</p>
<h3>Example 1</h3>
<table class="ex" cellspacing="0" border="1" width="100%">
<tr>
<td>
<pre>&lt;script type=&quot;text/javascript&quot;&gt;
//Write a &quot;Good morning&quot; greeting if
//the time is less than 10</pre>
<pre>var d=new Date();
var time=d.getHours();

if (time&lt;10) 
{
document.write(&quot;&lt;b&gt;Good morning&lt;/b&gt;&quot;);
}
&lt;/script&gt;</pre>
</td>
</tr>
</table>
<h3>Example 2</h3>
<table class="ex" cellspacing="0" border="1" width="100%" id="table5">
<tr>
<td>
<pre>&lt;script type=&quot;text/javascript&quot;&gt;
//Write &quot;Lunch-time!&quot; if the time is 11</pre>
<pre>var d=new Date();
var time=d.getHours();

if (time==11) 
{
document.write(&quot;&lt;b&gt;Lunch-time!&lt;/b&gt;&quot;);
}
&lt;/script&gt;</pre>
</td>
</tr>
</table>
<p><b>Note:</b> When <b>comparing</b> variables you must always use two equals 
signs next to each other (==)!</p>
<p>Notice that there is no ..else.. in this syntax. You just tell the code to
execute some code <b>only if the specified condition is true</b>.</p>
<hr />

<h2>If...else Statement</h2>
<p>If you want to execute some code if a condition is true and another code if 
the condition is not true,
use the if....else statement.</p>
<h3>Syntax</h3>
<table class="ex" cellspacing="0" border="1" width="100%">
<tr>
<td>
<pre>if (<i>condition</i>)
{
<i>code to be executed if condition is true</i>
}
else
{
<i>code to be executed if condition is not true</i>
}</pre>
</td>
</tr>
</table>
<h3>Example</h3>
<table class="ex" cellspacing="0" border="1" width="100%">
<tr>
<td>
<pre>&lt;script type=&quot;text/javascript&quot;&gt;
//If the time is less than 10,
//you will get a &quot;Good morning&quot; greeting.
//Otherwise you will get a &quot;Good day&quot; greeting.</pre>
<pre>var d = new Date();
var time = d.getHours();

if (time &lt; 10) 
{
document.write(&quot;Good morning!&quot;);
}
else
{
document.write(&quot;Good day!&quot;);
}
&lt;/script&gt;</pre>
</td>
</tr>
</table>
<br />
<hr />

<h2>If...else if...else Statement</h2>
<p>You should use the if....else if...else statement if you want to select one 
of many sets of lines to execute.</p>
<h3>Syntax</h3>
<table class="ex" cellspacing="0" border="1" width="100%" id="table3">
<tr>
<td>
<pre>if (<i>condition1</i>)
{
<i>code to be executed if condition1 is true</i>
}
else if (<i>condition2</i>)
{
<i>code to be executed if condition2 is true</i>
}
else
{
<i>code to be executed if condition1 and
condition2 are not true</i>
}</pre>
</td>
</tr>
</table>
<h3>Example</h3>
<table class="ex" cellspacing="0" border="1" width="101%" id="table4">
<tr>
<td>
<pre>&lt;script type=&quot;text/javascript&quot;&gt;
var d = new Date()
var time = d.getHours()
if (time&lt;10)
{
document.write(&quot;&lt;b&gt;Good morning&lt;/b&gt;&quot;);
}
else if (time&gt;10 &amp;&amp; time&lt;16)
{
document.write(&quot;&lt;b&gt;Good day&lt;/b&gt;&quot;);
}
else
{
document.write(&quot;&lt;b&gt;Hello World!&lt;/b&gt;&quot;);
}
&lt;/script&gt;</pre>
</td>
</tr>
</table>
<br />
<hr />

<a href="js_comparisons.asp"><img alt="previous" border="0" src="../images/btn_previous.gif" width="100" height="20" /></a>
<a href="js_switch.asp"><img alt="next" border="0" src="../images/btn_next.gif" width="100" height="20" /></a>

<p>From <b>http://www.w3schools.com</b> (Copyright Refsnes Data)</p>

</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -