📄 day4_12.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 face="宋体" size="3"><nobr>该HTML页含有一个叫做announceTime的函数。从一个链接调用<br>
annoumnceTime:</nobr></font></p>
<p><nobr><font face="宋体"><br>
<a href="#" onClick="announceTime();">time!</a><br>
<br>
</font></nobr></p>
<nobr><p ALIGN="JUSTIFY"><font face="宋体" SIZE="3">就象这样:</font></p>
<font SIZE="3"><p ALIGN="JUSTIFY"><font face="宋体">下行看起来就象第二课:</font></p>
</font></nobr><p ALIGN="JUSTIFY"><nobr><font face="宋体"><br>
<a href="#" onClick="alert('Hello!');">Hello!</a><br>
<br>
</font></nobr></p>
<nobr><p ALIGN="JUSTIFY"><font face="宋体" SIZE="3">这称为从一个链接调用警告对话框。函数就象一种方法,唯一不<br>
同的是,方法依附于一个对象。在这个警告的例子中,这个对象<br>
是一个窗口对象。</font></p>
<font SIZE="3"><p ALIGN="JUSTIFY"><font face="宋体">让我们回到函数本身。如果你看看源码,你将看到函数位于HTML<br>
文件的头部中。</font></p>
</font></nobr><p ALIGN="JUSTIFY"><font face="宋体"><nobr><br>
<html><br>
<br>
<head><br>
<br>
<title>Function with No Parameters</title><br>
<br>
<script langauge="JavaScript"><br>
<br>
<!-- hide me<br>
<br>
function announceTime()<br>
<br>
{<br>
<br>
//get the date, the hour, minutes, and seconds<br>
<br>
var the_date = new Date();<br>
<br>
var the_hour = the_date.getHours();<br>
<br>
var the_minute = the_date.getMinutes();<br>
<br>
var the_second = the_date.getSeconds();<br>
<br>
<br>
//put together the string and alert with it<br>
<br>
var the_time = the_hour + ":" + the_minute <br>
+ ":" + the_second;<br>
<br>
alert("The time is now: " + the_time);<br>
<br>
}<br>
<br>
<br>
// show me --><br>
<br>
</script><br>
<br>
<br>
</head><br>
<br>
<body><br>
<br>
...<br>
<br>
</body><br>
<br>
</html><br>
<br>
<font SIZE="3">好,让我们逐行复习这个函数。首先,所有函数来自于该种格式:</font></nobr></font></p>
<p ALIGN="JUSTIFY"><nobr><font face="宋体"><br>
function functionName(parameter list)<br>
<br>
{<br>
<br>
statements ...<br>
<br>
}<br>
<br>
</font></nobr></p>
<nobr><p ALIGN="JUSTIFY"><font face="宋体" SIZE="3">函数的命名规则于变量差不多。第一个字符必须是字母或一标准符<br>
号。其余字符可为数字或一横线。但必须保证函数不于已定义的变<br>
量同名。否则将出现很糟糕的结果。我是用内部大写的方式命名函<br>
数以保证它们不与字符碰巧重名。</font></p>
<font SIZE="3"><p ALIGN="JUSTIFY"><font face="宋体">函数名后是一组参数。本例是无参数的函数,下一例中我们再举例<br>
描述。</font></p>
<p ALIGN="JUSTIFY"><font face="宋体">参数后是函数的主体。这是一组当函数调用后是想运行的语句。在<br>
下面几个例子中,我打算利用这个报时器,所以让我描述一下它是<br>
怎样工作的。</font></p>
<p ALIGN="JUSTIFY"><font face="宋体">第一行:</font></p>
</font></nobr><p ALIGN="JUSTIFY"><nobr><font face="宋体"><br>
var the_date = new Date();<br>
<br>
</font></nobr></p>
<nobr><font SIZE="3"><p ALIGN="JUSTIFY"><font face="宋体">取得一个新的日期对象。就象你在用数组时取得一个新的数组一<br>
样,在你要找出即时是什么时间时你需要先取得一个日期对象。<br>
当找到了一个新的日期对象,它自动重置到当前的日期和时间。<br>
为了在对象以外得到这个信息,你必须使用这种对象方法:</font></p>
<p ALIGN="JUSTIFY"><font face="宋体">这些方法从日期对象上取得了合适的数字。</font></p>
</font></nobr><p ALIGN="JUSTIFY"><nobr><font face="宋体" size="2"><br>
var the_hour = the_date.getHours();<br>
<br>
var the_minute = the_date.getMinutes();<br>
<br>
var the_second = the_date.getSeconds();<br>
<br>
</font></nobr></p>
<nobr><p ALIGN="JUSTIFY"><font face="宋体" size="3">你可能疑惑:我怎样能假定日期对象知道何种方式?甚或我如何<br>
知道有这样一件事可作为日期对象?这些缘由应从Javascript<br>
库中获取,我将尽我所能解释内置Javascript对象,但不一定能<br>
彻底的使你清楚。</font></p>
<p ALIGN="JUSTIFY"><font face="宋体" size="3">函数的其他部分就很清楚了。它以这种方式调用返回数字,把它<br>
们变成字符串,并且调用警告方式以弹出一个字符串对话框。注<br>
意你可以在函数内部调用一个方式和函数。我们将详尽讨论。</font></p>
<p ALIGN="JUSTIFY"><font face="宋体" size="3">现在如果你也玩透了时间链接,你可能注意到了有些什么不对的<br>
事。你每次可能会得到这样的反馈:“12:12:04”,这是<br>
getSecond()将返回值为“4”。那么当你合成为时间时,你看到<br>
的就是the_minute+“:”+the_second得到14:4而非是我们想要<br>
的。解决它是个容易的事,需要个新的函数来修补分、秒合成值。</font></p>
<p ALIGN="JUSTIFY"><font face="宋体" size="3">请看</nobr><a href="day4_13.html" tppabs="http://www.pchome.net/ch/tur/pstext/teacher/javascript/day4_13.html">参数及返回值</a><nobr>。</nobr></font></p>
<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_12.html" tppabs="http://www.pchome.net/ch/tur/pstext/teacher/javascript/day4_12.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://www.pchome.net/ch/tur/pstext/teacher/javascript/day4_2.html">循环介绍</a></font><font
size="2"><br>
</font><font SIZE="3"><font color="#FF0000">第三页</font> <a href="day4_3.html" tppabs="http://www.pchome.net/ch/tur/pstext/teacher/javascript/day4_3.html">循环的密码</a></font><font
size="2"><br>
</font><font SIZE="3"><font color="#FF0000">第四页</font> <a href="day4_4.html" tppabs="http://www.pchome.net/ch/tur/pstext/teacher/javascript/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://www.pchome.net/ch/tur/pstext/teacher/javascript/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://www.pchome.net/ch/tur/pstext/teacher/javascript/day4_6.html">嵌套循环</a></font><font
size="2"><br>
</font><font SIZE="3"><font color="#FF0000">第七页</font> <a href="day4_7.html" tppabs="http://www.pchome.net/ch/tur/pstext/teacher/javascript/day4_7.html">循环练习</a></font><font
size="2"><br>
</font><font SIZE="3"><font color="#FF0000">第八页</font> <a href="day4_8.html" tppabs="http://www.pchome.net/ch/tur/pstext/teacher/javascript/day4_8.html">数组</a></font><font
size="2"><br>
</font><font SIZE="3"><font color="#FF0000">第九页</font> <a href="day4_9.html" tppabs="http://www.pchome.net/ch/tur/pstext/teacher/javascript/day4_9.html">数组和循环</a></font><font
size="2"><br>
</font><font SIZE="3"><font color="#FF0000">第十页</font> <a href="day4_10.html" tppabs="http://www.pchome.net/ch/tur/pstext/teacher/javascript/day4_10.html">文件目标模块中的数组</a></font><font
size="2"><br>
</font><font SIZE="3"><font color="#FF0000">第十一页</font> <a href="day4_11.html" tppabs="http://www.pchome.net/ch/tur/pstext/teacher/javascript/day4_11.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_13.html" tppabs="http://www.pchome.net/ch/tur/pstext/teacher/javascript/day4_13.html">参数及返回值</a></font><font
size="2"><br>
</font><font SIZE="3"><font color="#FF0000">第十四页</font> <a href="day4_14.html" tppabs="http://www.pchome.net/ch/tur/pstext/teacher/javascript/day4_14.html">多于一个参数的函数</a></font></p>
<p align="left"><font face="宋体" size="3">[<a href="index.html" tppabs="http://www.pchome.net/ch/tur/pstext/teacher/javascript/index.html">第1课</a>][<a
href="day2_1.html" tppabs="http://www.pchome.net/ch/tur/pstext/teacher/javascript/day2_1.html">第2课</a>][<a href="day3_1.html" tppabs="http://www.pchome.net/ch/tur/pstext/teacher/javascript/day3_1.html">第3课</a>][第4课][<a
href="day5_1.html" tppabs="http://www.pchome.net/ch/tur/pstext/teacher/javascript/day5_1.html">第5课</a>]</font></p>
<hr align="left">
<p><font size="2">本文由<a href="javascript:if(confirm('http://chd.126.com/ \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address. \n\nDo you want to open it from the server?'))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\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address. \n\nDo you want to open it from the server?'))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\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address. \n\nDo you want to open it from the server?'))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 + -