我有六个<div class="gallery-popup">元素。在每个<a href="...">标签中都有缩略图。我想要的是,将属性data-lightbox="gallery-X"添加到每个<a>元素中,但要针对这6个库中的每一个添加属性。例如,gallery-1对于第一个.gallery-popup中的每个<a>,第二个.gallery-popup中的gallery-2等等。我现在掌握的代码是:
var thumbnailInterval = setInterval(function () {
if ($('.offer .box_info_potos div .gallery-popup li a').length) {
console.log('found' + $('.offer ').length);
clearInterval(thumbnailInterval);
$('.offer .gallery-popup a').each(function (index) {
var a = $(this).get()[0];
a.setAttribute('data-lightbox', 'gall-' + index);
console.log(a);
});
}
}, 100); 发布于 2017-07-27 13:35:12
尝尝这个
$('.offer .gallery-popup').each(function (index,item) {
$(item).find('a').attr('data-lightbox','gall-'+(index+1));
});更新
看到这张图片我的代码就这样做了。

发布于 2017-07-27 13:35:01
试着像这样
$('.offer .gallery-popup ').find('a').each(function (index,div) {
div.setAttribute('data-lightbox', 'gall-' + index);
});https://stackoverflow.com/questions/45352266
复制相似问题