为什么只定位给定对象中的最后一项?

时间:2016-12-28 05:16:38

标签: jquery

enter image description here

上面显示了包含对象的模板,如firebug中的控制台选项卡所示

console.debug(mimicTemplate);

//mimicTemplate is a template containing two or more similar objects.

$.each(mimicTemplate, function (index, value) {

//only last object is targetted                             
$(value).find(config.selectors.elementId).css('background-color', '#c8e6c9');
$(value).find(config.selectors.elementId).data('id', attachment[i].fileId);
});

预计,模板中的每个对象都应该被定位

尝试:

    $.each(mimicTemplate, function (index, value) {
    console.debug(attachment[i].fileId);
    $(value).parent().children().toggle().find(config.selectors.elementId).css('background-color', '#c8e6c9');
    $(value).parent().children().toggle().find(config.selectors.elementId).data('id', attachment[i].fileId);
});


$(value).parent().children().toggle() targets each elements, but `.data('id', attachment[i].fileId); is replaced with last value for each element`.

console.debug(attachment[i].fileId); shows each id correctly. Only when it comes to assigning the value to the data attribbute, it gets the last item and replaces all elments.

0 个答案:

没有答案