1.函数
<html>
<head>
<script type="text/javascript">
function myfunction()
{
alert("您好!")
}
</script>
</head>
<body>
<form>
<input type="button" value="调用函数">
</form>
<p>通过点击这个按钮,可以调用一个函数。该函数会提示一条消息。</p>
</body>
</html>
2.带参数的函数
<html>
<head>
<script type="text/javascript">
function myfunction(txt)
{
alert(txt)
}
</script>
</head>
<body>
<form>
<input type="button"
value="在早晨">
<input type="button"
value="在夜晚">
</form>
<p>通过点击这个按钮,可以调用一个函数。该函数会输出传递给它的参数。</p>
</body>
</html>
3.带返回值的函数
<html>
<head>
<script type="text/javascript">
function myFunction()
{
return ("您好,祝您愉快!")
}
</script>
</head>
<body>
<script type="text/javascript">
document.write(myFunction())
</script>
<p>body 部分中的脚本调用一个函数。</p>
<p>该函数返回一段文本。</p>
</body>
</html>
4.同时带参数和返回值的函数
<html>
<head>
<script type="text/javascript">
function product(a,b)
{
return a*b
}
</script>
</head>
<body>
<script type="text/javascript">
document.write(product(6,5))
</script>
<p>body 部分中的脚本调用一个带有两个参数(6 和 5)的函数。</p>
<p>该函数会返回这两个参数的乘积。</p>
</body>
</html>