【发布时间】:2015-06-05 02:35:06
【问题描述】:
我有类似这样的代码:
for (i = 0; i < imageData.length; i++) {
for (j = 0; j < sizes.length; j++){
width = sizes[j][0];
height = sizes[j][1];
objectId = 'cropper-' + i + '-' + j;
var $image = $('#' + cropperId + ' > img'),
$preview = $('#preview_cropper-' + i + '-' + j),
$btn = $('strict-mode_cropper-' + i + '-' + j);
var options = {
...
width: width,
height: height,
$preview: $preview,
...
};
$btn.on("click", {$image: $image, options: options}, foo);
}
}
function foo(e) {
var $image = e.data.$image; // Gives the correct Image
var $preview = e.data.options.$preview // Always the last $preview. Not the one that was passed in!
var width = e.data.options.width //Also incorrect
var height = e.data.options.height //Also incorrect
}
问题在于,当用户单击$btn 之一时,“选项”中的所有字段始终是嵌套循环的最后一个(即当 i = imageData.length - 1 和 j = size.长度 - 1)。 #
奇怪的是,传入的$image 是正确的!
为什么会这样?我该如何解决这个问题?
【问题讨论】:
-
您没有发布设置
$preview变量的代码。
标签: javascript jquery parameter-passing jquery-events