如何使用 JavaScript 调用函数?
函数是一组在程序的任何地方均可调用的可重用代码。这避免了重复书写相同代码的需要。它可帮助程序员编写模块化代码。
在 JavaScript 中定义函数最常见的方法是使用 function 关键字,后跟一个唯一的函数名称、一个参数列表(可能为空)以及用花括号括起来的一个语句块。
以下是一个示例 −
<script>
<!--
function sayHello() {
alert("Hello there");
}
//-->
</script>若要在脚本之后的某个位置调用函数,您只需书写该函数的名称,如下面的代码所示。
范例
这是一个显示如何在 JavaScript 中调用函数的示例
<html>
<head>
<script>
function sayHello() {
d ocument.write ("Hello there!");
}
</script>
</head>
<body>
<p>Click the following button to call the function</p>
<form>
<input type = "button" onclick = "sayHello()" value = "Say Hello">
</form>
<p>Use different text in write method and then try...</p>
</body>
</html>
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP