|
第十二页:无参数函数
该HTML页含有一个叫做announceTime的函数。从一个链接调用
annoumnceTime:
<a href="#" onClick="announceTime();">time!</a>
就象这样:
下行看起来就象第二课:
<a href="#" onClick="alert('Hello!');">Hello!</a>
这称为从一个链接调用警告对话框。函数就象一种方法,唯一不
同的是,方法依附于一个对象。在这个警告的例子中,这个对象
是一个窗口对象。
让我们回到函数本身。如果你看看源码,你将看到函数位于HTML
文件的头部中。
<html>
<head>
<title>Function with No Parameters</title>
<script langauge="JavaScript">
<!-- hide me
function announceTime()
{
//get the date, the hour, minutes, and seconds
var the_date = new Date();
var the_hour = the_date.getHours();
var the_minute = the_date.getMinutes();
var the_second = the_date.getSeconds();
//put together the string and alert with it
var the_time = the_hour + ":" + the_minute
+ ":" + the_second;
alert("The time is now: " + the_time);
}
// show me -->
</script>
</head>
<body>
...
</body>
</html> |
|