【发布时间】:2012-12-15 17:14:36
【问题描述】:
可能重复:
How do I pass the value (not the reference) of a JS variable to a function?
我有几个链接(var a[]),上面有一个 onclick 事件。到目前为止,使用一个功能效果很好:
function setCurrentImage(x) {
return function() {
alert("Image: "+x);
return false;
}
}
for(var i = 0; i < a.length; i++) {
a[i].onclick = setCurrentImage(i);
}
问题是,我需要两个函数。并且这段代码不起作用:
function setCurrentImage(x) {
return function() {
alert("Image: "+x);
}
}
function setCurrentNote(x) {
return function() {
alert("Note: "+x);
}
}
for(var i = 0; i < a.length; i++) {
a[i].onclick = function() {
setCurrentImage(i);
setCurrentNote(i);
return false;
}
}
这段代码有什么问题?在此先感谢:)
【问题讨论】:
-
为什么不起作用?会发生什么?
标签: javascript function onclick return