JavaScript 中可以把方法名作为参数传递, 这样可以在一个统一的方法中调用不同的函数,如下:
<!DOCTYPE html>
<html>
<head>
<title>test.html</title>
<meta name="keywords" content="keyword1,keyword2,keyword3">
<meta name="description" content="this is my page">
<meta name="content-type" content="text/html; charset=GBK">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
<script type="text/javascript">
function test1(name) {
alert("这是测试方法1"+name);
}
function test2() {
alert("这是测试方法2");
}
function test3() {
var test = test1;
test("张三");
}
function test4(test) {
test("李四");
}
</script>
</head>
<body>
This is my HTML page.
<br>
<input type="button" value="test4调用test1" οnclick="test4(test1)" />
<input type="button" value="test4调用test2" οnclick="test4(test2)" />
<input type="button" value="test3调用test1" οnclick="test3()" />
</body>
</html>