我希望动态地(通过jSon、XML、静态数组等提供的数据)构建一系列div,这些div最终包含锚标记(或按钮),其中包含图像。
这是我目前掌握的情况:
$(document).ready(function () {
let testButtonEntries = [
{ button_title: 'Employee Button Title', button_alt_text: 'Employee Alt Text', button_image: 'emp_button_title', link: 'insertsomelinkhere' },
{ button_title: 'Employee Button Title', button_alt_text: 'Employee Alt Text', button_image: 'emp_button_title', link: 'somelinktosomewhere.com/' },
{ button_title: 'Employee Button Title', button_alt_text: 'Employee Alt Text', button_image: 'emp_button_title', link: 'somelinktosomewhere.com/' },
{ button_title: 'Employee Button Title', button_alt_text: 'Employee Alt Text', button_image: 'emp_button_title', link: 'somelinktosomewhere.com/' },
{ button_title: 'Employee Button Title', button_alt_text: 'Employee Alt Text', button_image: 'emp_button_title', link: 'somelinktosomewhere.com/' },
{ button_title: 'Employee Button Title', button_alt_text: 'Employee Alt Text', button_image: 'emp_button_title', link: 'somelinktosomewhere.com/' },
{ button_title: 'Employee Button Title', button_alt_text: 'Employee Alt Text', button_image: 'emp_button_title', link: 'somelinktosomewhere.com/' },
{ button_title: 'Employee Button Title', button_alt_text: 'Employee Alt Text', button_image: 'emp_button_title', link: 'somelinktosomewhere.com/' },
{ button_title: 'Employee Button Title', button_alt_text: 'Employee Alt Text', button_image: 'emp_button_title', link: 'somelinktosomewhere.com/' },
{ button_title: 'Employee Button Title', button_alt_text: 'Employee Alt Text', button_image: 'emp_button_title', link: 'somelinktosomewhere.com/' },
{ button_title: 'Employee Button Title', button_alt_text: 'Employee Alt Text', button_image: 'emp_button_title', link: 'somelinktosomewhere.com/' },
{ button_title: 'Employee Button Title', button_alt_text: 'Employee Alt Text', button_image: 'emp_button_title', link: 'somelinktosomewhere.com/' },
{ button_title: 'Employee Button Title', button_alt_text: 'Employee Alt Text', button_image: 'emp_button_title', link: 'somelinktosomewhere.com/' },
{ button_title: 'Employee Button Title', button_alt_text: 'Employee Alt Text', button_image: 'emp_button_title', link: 'somelinktosomewhere.com/' },
{ button_title: 'Employee Button Title', button_alt_text: 'Employee Alt Text', button_image: 'emp_button_title', link: 'somelinktosomewhere.com/' },
{ button_title: 'Employee Button Title Last', button_alt_text: 'Employee Alt Text Last', button_image: 'emp_button_title_last', link: 'insertsomelinkhere.org' }
];
for (let sections = 0; sections <= testButtonEntries.length % 15; sections++) {
$('<div class="carousel-item">')
.append($('<div class="d-flex flex-wrap"/>').append(function () {
for (let i = 0; i < 15; i++) {
return $('<div/>')
.append($('<a/>', {
href: testButtonEntries[i].link,
title: testButtonEntries[i].button_title,
target: '_blank'
}).append($('<img/>', {
alt: testButtonEntries[i].button_alt_text,
class: 'img-responsive',
src: 'images/' + testButtonEntries[i].button_image + '.png'
})));
}
}))
.appendTo($('#employee-button-container'));
}
});
它产生了我想要的结果,但是(因为很明显return语句会返回第一个结果,这是一个 Package )它没有在第一部分中给我15个“按钮”,在第二部分中给我一个我正在努力的按钮。
2条答案
按热度按时间i86rm4rw1#
我们可以使用slice方法将主数组划分/拆分为单独的数组块(每个块有15个元素),并使用map构建HTML
iqjalb3h2#
这是我一直在寻找的答案(只需要睡在它LOL)
在我最初的回答中,我使用的是模数,这绝对是错误的(再次...显然需要睡眠,哈哈),以获得建立按钮组所需的通行证数量。相反,我只是使用Math.ceil()来舍入返回的任何数字,这样我就有了正确的通道数来创建每个部分。至于将按钮/链接本身附加到每个部分,我需要创建一个变量来保存所有15个正在构建的按钮/链接,然后让函数返回变量作为append()的部分。而且......这就是我拍摄的方式。:)