📄 day4_9.html
字号:
<html><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312-80"><style type="text/css"><!--a:link { color: blue; text-decoration: none}a:visited { color: purple; text-decoration: none}a:hover { color: #CC0033; text-decoration: underline}--></style><title>JavaScript教程</title></head><body topmargin="1" leftmargin="2"><table border="0" width="591" cellspacing="0"> <tr> <td bgcolor="#ffff99" width="451">JavaScript教程 - 第四课</td> </tr> <tr> <td bgcolor="#FF6600" width="451"><a href="mailto:thau@wired.com">Thau</a></td> </tr></table><div align="left"><table border="0" width="630" cellspacing="0"> <tr> <td width="458" valign="top" align="left"><strong><small><small><br> </small></small><font face="宋体" size="3">第九页:</font><font SIZE="3">数组和循环</font></strong> <p><font SIZE="3">数组是一项非常有用的东西,因为你可以循环调用数组中的各<br> 个元素执行某项功能。以下是循环显示一个<a href="javascript:if(confirm('http://www.hotwired.com/webmonkey/98/04/files1a/slideshow.html \n\n文件并未依 Teleport Pro 取回,因为 它的域或路径超过开始网址中设置的范围。 \n\n你要从服务器上打开它吗?'))window.location='http://www.hotwired.com/webmonkey/98/04/files1a/slideshow.html'" tppabs="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" tppabs="http://sun150-2.fimmu.edu.cn/doing/js1/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" tppabs="http://sun150-2.fimmu.edu.cn/doing/js1/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" tppabs="http://sun150-2.fimmu.edu.cn/doing/js1/day4_2.html">循环介绍</a></font><font size="2"><br> </font><font SIZE="3"><font color="#FF0000">第三页</font> <a href="day4_3.html" tppabs="http://sun150-2.fimmu.edu.cn/doing/js1/day4_3.html">循环的密码</a></font><font size="2"><br> </font><font SIZE="3"><font color="#FF0000">第四页</font> <a href="day4_4.html" tppabs="http://sun150-2.fimmu.edu.cn/doing/js1/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" tppabs="http://sun150-2.fimmu.edu.cn/doing/js1/day4_5.html">For循环</a></font><font size="2"><br> </font><font SIZE="3"><font color="#FF0000">第六页</font> <a href="day4_6.html" tppabs="http://sun150-2.fimmu.edu.cn/doing/js1/day4_6.html">嵌套循环</a></font><font size="2"><br> </font><font SIZE="3"><font color="#FF0000">第七页</font> <a href="day4_7.html" tppabs="http://sun150-2.fimmu.edu.cn/doing/js1/day4_7.html">循环练习</a></font><font size="2"><br> </font><font SIZE="3"><font color="#FF0000">第八页</font> <a href="day4_8.html" tppabs="http://sun150-2.fimmu.edu.cn/doing/js1/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" tppabs="http://sun150-2.fimmu.edu.cn/doing/js1/day4_10.html">文件目标模块中的数组</a></font><font size="2"><br> </font><font SIZE="3"><font color="#FF0000">第十一页</font> <a href="day4_11.html" tppabs="http://sun150-2.fimmu.edu.cn/doing/js1/day4_11.html">函数</a></font><font size="2"><br> </font><font SIZE="3"><font color="#FF0000">第十二页</font> <a href="day4_12.html" tppabs="http://sun150-2.fimmu.edu.cn/doing/js1/day4_12.html">无参数函数</a></font><font size="2"><br> </font><font SIZE="3"><font color="#FF0000">第十三页</font> <a href="day4_13.html" tppabs="http://sun150-2.fimmu.edu.cn/doing/js1/day4_13.html">参数及返回值</a></font><font size="2"><br> </font><font SIZE="3"><font color="#FF0000">第十四页</font> <a href="day4_14.html" tppabs="http://sun150-2.fimmu.edu.cn/doing/js1/day4_14.html">多于一个参数的函数</a></font></p> <p align="left"><font face="宋体" size="3">[<a href="index.html" tppabs="http://sun150-2.fimmu.edu.cn/doing/js1/index.html">第1课</a>][<a href="day2_1.html" tppabs="http://sun150-2.fimmu.edu.cn/doing/js1/day2_1.html">第2课</a>][<a href="day3_1.html" tppabs="http://sun150-2.fimmu.edu.cn/doing/js1/day3_1.html">第3课</a>][第4课][<a href="day5_1.html" tppabs="http://sun150-2.fimmu.edu.cn/doing/js1/day5_1.html">第5课</a>]</font></p> <hr align="left"> <p><font size="2">本文由<a href="javascript:if(confirm('http://chd.126.com/ \n\n文件并未依 Teleport Pro 取回,因为 它的域或路径超过开始网址中设置的范围。 \n\n你要从服务器上打开它吗?'))window.location='http://chd.126.com/'" tppabs="http://chd.126.com/" target="_blank">《</font><font face="verdana, arial, geneva, sans-serif" size="2">CHD</font><font size="2">的网络教室》</a>根据<a href="javascript:if(confirm('http://www.webmonkey.com.cn/ \n\n文件并未依 Teleport Pro 取回,因为 它的域或路径超过开始网址中设置的范围。 \n\n你要从服务器上打开它吗?'))window.location='http://www.webmonkey.com.cn/'" tppabs="http://www.webmonkey.com.cn/" target="_blank">《网猴》</a>相关文章改编,版权归<a href="javascript:if(confirm('http://www.webmonkey.com.cn/ \n\n文件并未依 Teleport Pro 取回,因为 它的域或路径超过开始网址中设置的范围。 \n\n你要从服务器上打开它吗?'))window.location='http://www.webmonkey.com.cn/'" tppabs="http://www.webmonkey.com.cn/" target="_blank">《网猴》</a>所有</font></td> </tr></table></div></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -