JavaScript 函数允许没有参数(但包含参数的小括号不能省略),也可以向函数传递参数供函数使用。
下面的例子中,就向 hello() 函数传递了 name 和 age 参数,参数值分别是 小明 和 18。
<html>
<head>
<script type="text/javascript">
function hello(name,age){
document.write("我叫" + name + ",今年" + age + "岁!");
}
</script>
</head>
<body>
<input type="button" onclick="hello('小明',18)" value="确定" />
</body>
</html>
运行该例子,点击 确定 按钮,输出:
我叫小明,今年18岁!
如上例所示,传入的值是字符串时,需要加上引号;传入的值是数字,不需要加引号。