我有两个循环,我想循环遍历loop1并将其数据属性值插入loop2。使用下面的方法我该怎么做?
谢谢!
$(".groupA[data-background]").each(function(i){
var backgroundData = $(this).data('background');
})
$(".groupB").each(function(i){
var newBackground = $(this).attr('style'); ???
})
编辑:我尝试了嵌套循环,但它只是重复相同的'backgroundData'。我想念什么?
$(".swatch[data-background]").each(function(i){
var backgroundData = $(this).data('background');
console.log(backgroundData);
$('swatch').each(function(i){
$(this).attr('style', backgroundData )
})
})
您可以使用map并在第二个循环中将值保存在数组中,例如:
var backgroundData = $(".groupA[data-background]").map(function(i) {
return $(this).data('background');
});
$(".groupB").each(function(i) {
var newBackground = $(this).attr('style', "background:" + backgroundData[i]);
});
参考
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句