1
我想在我的onclick上调用一个特定的函数数组。调用特定的函数阵列
例JS:
var foo = [
function() { alert("Function 1!"); },
function() { alert("Function 2!"); }
];
示例HTML:
<a onclick="foo[0]" >Alert</a>
,但不知道如何完成它。
我想在我的onclick上调用一个特定的函数数组。调用特定的函数阵列
例JS:
var foo = [
function() { alert("Function 1!"); },
function() { alert("Function 2!"); }
];
示例HTML:
<a onclick="foo[0]" >Alert</a>
,但不知道如何完成它。
你需要括号调用函数。否则,你例子2.
<a onclick="foo[0]()" >Alert</a>
<!-- ^^ -->
var foo = [
function() { alert("Function 1!"); },
function() { alert("Function 2!"); }
];
<a onclick="foo[0]()" >Alert</a><br>
<a onclick="alert(foo[0])" >Alert, what really happens without parenthesis</a>
你应该调用该函数。因为你只能使用foo[0]
。你需要()
打电话给他们,运行功能
var foo = [
function() { alert("Function 1!"); },
function() { alert("Function 2!"); }
];
foo[0]();
foo[1]();
在纯返回函数本身,就像是在你的脸上,愚蠢的我,非常感谢鼻子:) – SHi