📄 js_loop_for.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 for Loop</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 For Loop</h1>
<a href="js_functions.asp"><img alt="previous" border="0" src="../images/btn_previous.gif" width="100" height="20" /></a>
<a href="js_loop_while.asp"><img alt="next" border="0" src="../images/btn_next.gif" width="100" height="20" /></a>
<hr />
<p class="intro">Loops in JavaScript are used to execute the same block of code
a specified number of times or while a specified condition is true.</p>
<hr />
<h2>Examples</h2>
<p><a target="_blank" href="tryit.asp@filename=tryjs_fornext">For loop</a><br />
How to write a for loop. Use a For loop to run the same block of code a specified number of times.</p>
<p><a target="_blank" href="tryit.asp@filename=tryjs_fornext_header">Looping through HTML headers</a><br />
How to use the for loop to loop through the different HTML headers.</p>
<hr />
<h2>JavaScript Loops</h2>
<p>Very often when you write code, you want the same block of code to run
over and over again in a row. Instead of adding several almost equal lines in a script
we can use loops to
perform a task like this.</p>
<p>In JavaScript there are two different kind of loops:</p>
<ul>
<li><b>for </b>- loops through a block of code a specified number of times</li>
<li><b>while </b>- loops through a block of code while a specified condition
is true</li>
</ul>
<hr />
<h2>The for Loop</h2>
<p>The for loop is used when you know in advance how many times the script
should run.</p>
<p><b>Syntax</b></p>
<table class="ex" cellspacing="0" border="1" width="100%" id="table1">
<tr>
<td>
<pre>for (var=startvalue;var<=endvalue;var=var+increment)
{
<i>code to be executed</i>
}
</pre>
</td>
</tr>
</table>
<p><b>Example</b></p>
<p>Explanation: The example below defines a loop that starts with i=0. The loop
will continue to run as long as <b>i</b> is less than, or equal to 10. <b>i</b>
will increase by 1 each time the loop runs.</p>
<p><b>Note:</b> The increment parameter could also be negative, and the <=
could be any comparing statement.</p>
<table class="ex" cellspacing="0" border="1" width="100%" id="table2">
<tr>
<td>
<pre><html>
<body>
<script type="text/javascript">
var i=0;
for (i=0;i<=10;i++)
{
document.write("The number is " + i);
document.write("<br />");
}
</script>
</body>
</html></pre>
</td>
</tr>
</table>
<p><b>Result</b></p>
<table class="ex" cellspacing="0" border="1" width="100%" id="table3">
<tr>
<td>
<pre>The number is 0
The number is 1
The number is 2
The number is 3
The number is 4
The number is 5
The number is 6
The number is 7
The number is 8
The number is 9
The number is 10</pre>
</td>
</tr>
</table>
<br />
<hr />
<h2>The while loop</h2>
<p>The while loop will be explained in the next chapter.</p>
<hr />
<a href="js_functions.asp"><img alt="previous" border="0" src="../images/btn_previous.gif" width="100" height="20" /></a>
<a href="js_loop_while.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 + -