📄 day4_9.html
字号:
<tr>
<td colspan="2" align="center" bgcolor="#666699"><a href="../../1-teach/index.htm"><font color="#FFFFFF">实用技巧</font></a></td>
</tr>
<tr bgcolor="#FFFFFF">
<td colspan="2">-<a href="../../1-teach/internet/index.htm">Internet应用</a></td>
</tr>
<tr bgcolor="#FFFFFF">
<td colspan="2">-<a href="../../1-teach/photoshop/index.html">Photoshop</a></td>
</tr>
<tr bgcolor="#FFFFFF">
<td colspan="2">-<a href="../../1-teach/flash/page1.html">Flash</a></td>
</tr>
<tr bgcolor="#FFFFFF">
<td colspan="2">-<a href="../../1-teach/asp/index.html">ASP</a></td>
</tr>
<tr bgcolor="#FFFFFF">
<td colspan="2">-<a href="../../1-teach/php/index.html">PHP</a></td>
</tr>
<tr bgcolor="#FFFFFF">
<td colspan="2">-<a href="../../1-teach/java/index.htm">Java</a></td>
</tr>
<tr bgcolor="#FFFFFF">
<td colspan="2">-<a href="../../1-teach/vb/index.htm">VB</a></td>
</tr>
<tr bgcolor="#FFFFFF">
<td colspan="2">-<a href="../../1-teach/c/index.htm">C、C++</a></td>
</tr>
<tr bgcolor="#FFFFFF">
<td colspan="2">-<a href="../../1-backend/database/php_mysql/index.html">PHP/MySQL</a></td>
</tr>
<tr bgcolor="#FFFFFF">
<td colspan="2"><a href="../../1-backend/cgi_perl/perl_beginner/index.html">-Perl</a>
</td>
</tr>
<tr bgcolor="#FFFFFF">
<td colspan="2">-<a href="../../1-teach/other/index.htm">其它</a> </td>
</tr>
<tr>
<td colspan="2" bgcolor="#666699">
<div align="center"><font color="#FFFFFF">更多教程</font></div>
</td>
</tr>
<tr bgcolor="#FFFFFF">
<td colspan="2" height="17"><a href="../../1hdml/index.html">-HDML</a></td>
</tr>
<tr bgcolor="#FFFFFF">
<td colspan="2" height="23"><font face="宋体"><a href="../../1-backend/database/course/day1_1.html">-网络数据库</a></font></td>
</tr>
<tr bgcolor="#FFFFFF">
<td colspan="2" height="14"><a href="../../1-backend/protocols/ping/index.html"><font face="arial, helvetica, sans-serif">-ping</font></a></td>
</tr>
<tr bgcolor="#FFFFFF">
<td colspan="2" height="20"><a href="../../1-backend/cgi_perl/search_engine/index.html">-创建搜索引擎</a></td>
</tr>
<tr bgcolor="#FFFFFF">
<td colspan="2" height="16">-<a href="../../1adobe/GoLive/index.html">Adobe GoLive</a></td>
</tr>
<tr bgcolor="#FFFFFF">
<td colspan="2"><a href="../../1-backend/cgi_perl/templates/index.html">-模板</a></td>
</tr>
<tr bgcolor="#666699">
<td colspan="2" align="center"><font color="#FFFFFF">合作伙伴</font></td>
</tr>
<tr bgcolor="#FFFFFF">
<td colspan="2" align="left">-<a href="http://www.5dmedia.com/" target="_blank">5D精英网</a></td>
</tr>
<tr align="center" bgcolor="#FFFFFF">
<td colspan="2"> <img src="../../Library/front_monkey.gif" width="59" height="68"></td>
</tr>
</tbody>
</table>
<!-- #EndLibraryItem --> </td>
<!-- End of headlines (column 1: left column) --> <!-- Gutter between columns 1 and 2 -->
<td width="10" height="794"><img src="http://www.sohu.com/images/pixel.gif" width=10></td>
<!-- Search box and directories (columns 2 and 3: middle columns, combined into one) -->
<td align=center valign=top width="558">
<div align="left"><!-- #BeginEditable "1" -->
<title>JavaScrip教程</title>
<strong><font face="宋体" size="3">第九页:</font><font size="3">数组和循环</font></strong>
<p><font size="3">数组是一项非常有用的东西,因为你可以循环调用数组中的各<br>
个元素执行某项功能。以下是循环显示一个<a href="http://www.hotwired.com/webmonkey/98/04/files1a/slideshow.html" target="_blank"><font face="宋体">URL</font>数组中的各个元素</a><br>
的例子。</font></p>
<font size="3">
<p>首先,要使该例子发挥作用,我们需要声明一些变量:</p>
</font>
<p><font face="宋体"><br>
var url_names = new Array("hits.org","awaken.org","bianca.com");<br>
<br>
var a_url;<br>
<big><br>
</big>接下来,我们循环调用数组中的各个元素,打开每<small><br>
</small>个URL并对待用户点击alert框的OK按钮:</font></p>
<p><font face="宋体"><br>
for (loop = 0; loop <url_names.length; loop++)<br>
<br>
{<br>
<br>
// make the name of a url, for example http://www.hits.org/<br>
<br>
a_url = "http://www." + url_names[loop] + "/";<br>
<br>
// open a window<br>
<br>
var new_window=open(a_url,"new_window","width=300,height=300");<br>
<br>
// wait for the click<br>
<br>
alert("hit ok for the next site"); <br>
<br>
}<br>
<br>
</font></p>
<p><font size="3">首先,你会注意到循环从<font face="宋体">0</font>一直到<font face="宋体"><tt>url_names.length</tt></font>这个变量。<br>
将<font face="宋体"><tt>.length</tt></font>放在数组名的后面由于告诉你数组中有多少个元素。<br>
但是,注意数组元素的数目同数组最后一个元素的索引号(顺<br>
序号)不同。如果数组中有<font face="宋体">3</font>个元素,则数组的长度为<font face="宋体">3</font>,但是<br>
数组中最后一个元素的索引号却是<font face="宋体"><tt>array[2]</tt>.</font>。这是因为数组中<br>
第<font face="宋体">1</font>个元素的索引号是<font face="宋体"><tt>array[0]</tt>.</font>。如果你执行数组调用时得到<br>
诸如<font face="宋体">"object not found" </font>的错误信息,而你的代码中有一个<br>
数组,则有可能是因为你将数组元素的索引号同数组中元素的<br>
数目混淆了。</font></p>
<font size="3">
<p>不也许还会注意到将<font face="宋体"><tt>.length</tt></font>放在数组的结尾处很有点想给一个<br>
对象附加一些属性。这是因为数组本身就是对象,而<font face="宋体">length</font>就<br>
是数组的一项属性。</p>
<p>数组术语对象的另一个表现是:你需要用新指令才能生成新的<br>
数组。在上例中,<font face="宋体"><tt>url_names = new Array(......)</tt> </font>实际可以<br>
解释为:生成一个新数组,用<font face="宋体"><tt>url_names</tt></font>对其做一个引用。你可<br>
以注意到单词<font face="宋体">"new" </font>以这种方式被应用时,就生成了一个新的<br>
对象。</p>
<p>循环中的第<font face="宋体">1</font>行生成一个变量。其赋值为一个字符串。</p>
</font>
<p><font face="宋体">a_url = "http://www." + url_names[loop] + "/";<br>
<br>
</font></p>
<p><font size="3">循环开始时,变量循环的初始值为<font face="宋体">0</font>。<font face="宋体"><tt>url_names</tt></font>数组的第<font face="宋体">1</font>个元<br>
素是字符串<font face="宋体"><tt>"hits.org"</tt>.</font>,所以在第一次循环中,变量<font face="宋体"><tt>a_url</tt></font>等<br>
值于字符串<font face="宋体"><tt>"http://www.hits.org/"</tt>.</font>。</font></p>
<font size="3">
<p>循环的下一行用该<font face="宋体">URL</font>打开一个窗口 </p>
</font>
<p><font face="宋体">var new_window=open(a_url,"new_window","width=300,height=300");<br>
<big><br>
</big></font></p>
<p><font size="3">由于每次打开窗口时我们给窗口起的名称都一样,所以在转到<br>
新的<font face="宋体">URL</font>时,不会打开许多窗口。如果我们在上例中去掉窗口<br>
名称<font face="宋体"><tt>"new_window"</tt></font>,则每次循环时就会打开一个新窗口。
</font></p>
<font size="3">
<p>循环的第<font face="宋体">3</font>行只是打开一个<font face="宋体">alert</font>框,并对待用户点击<font face="宋体">OK
</font>按钮。 </p>
<p>数组还可以由于其它元素,而不只是字符串。数组可以应用<br>
于<font face="宋体">JavaScript</font>文件对象模块(<font face="宋体">Document Object
Model</font>)的各个<br>
方面。<a href="day4_10.html">>></a></p>
</font>
<p align="left"><font face="宋体" size="3" color="#000000"><strong>JavaScript教程</strong></font><font color="#FF0000" face="宋体" size="3"><br>
</font><font size="3" color="#FF0000">第一页</font><font size="2" face="宋体">
</font><a href="day4_9.html"><font size="3">第四课介绍</font></a><font size="2"><br>
</font><font size="3"><font color="#FF0000">第二页</font> <a href="day4_2.html">循环介绍</a></font><font size="2"><br>
</font><font size="3"><font color="#FF0000">第三页</font> <a href="day4_3.html">循环的密码</a></font><font size="2"><br>
</font><font size="3"><font color="#FF0000">第四页</font> <a href="day4_4.html">再谈<font face="宋体">WHILE</font>循环</a></font><font size="2"><br>
</font><font size="3"><font color="#FF0000">第五页</font> <a href="day4_5.html">For循环</a></font><font size="2"><br>
</font><font size="3"><font color="#FF0000">第六页</font> <a href="day4_6.html">嵌套循环</a></font><font size="2"><br>
</font><font size="3"><font color="#FF0000">第七页</font> <a href="day4_7.html">循环练习</a></font><font size="2"><br>
</font><font size="3"><font color="#FF0000">第八页</font> <a href="day4_8.html">数组</a></font><font size="2"><br>
</font><font size="3"><font color="#FF0000">第九页</font> 数组和循环</font><font size="2"><br>
</font><font size="3"><font color="#FF0000">第十页</font> <a href="day4_10.html">文件目标模块中的数组</a></font><font size="2"><br>
</font><font size="3"><font color="#FF0000">第十一页</font> <a href="day4_11.html">函数</a></font><font size="2"><br>
</font><font size="3"><font color="#FF0000">第十二页</font> <a href="day4_12.html">无参数函数</a></font><font size="2"><br>
</font><font size="3"><font color="#FF0000">第十三页</font> <a href="day4_13.html">参数及返回值</a></font><font size="2"><br>
</font><font size="3"><font color="#FF0000">第十四页</font> <a href="day4_14.html">多于一个参数的函数</a></font></p>
<p align="left"><font face="宋体" size="3">[<a href="index.html">第1课</a>][<a href="day2_1.html">第2课</a>][<a href="day3_1.html">第3课</a>][第4课][<a href="day5_1.html">第5课</a>]</font></p>
<!-- #EndEditable --></div>
</td>
<!-- End of search box and directories (columns 2 and 3: middle columns, combined into one) -->
<!-- Gutter between columns 3 and 4 --> <!-- Other stuff (column 4: right column) -->
<!-- End of other stuff (column 4: right column) --> </tr>
</table>
<!-- End of table surrounding page contents -->
<hr noshade size=1 width=700>
<span class=eng><br>
Copyright (C) 1998-2000 Internet Technologies China. All rights reserved.
</span>
</center>
</body>
<!-- #EndTemplate --></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -