<分区>
Possible Duplicate:
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;
}
}
这段代码有什么问题?提前致谢:)