我想知道最好的方法来使用JavaScript将数组中创建的每一项新的HTML元素(或者,如果有一个更好的方法也许不是使用数组?从数组添加选项值和内部HTML或对象
我将如何运行,对于阵列中的每个项目创建一个新的option,然后指定URL作为期权价值的循环
我的JS如下:。
var images = [
"Sports 1",
"http://lorempixel.com/50/50/sports/1",
"Sports 2",
"http://lorempixel.com/50/50/sports/2",
"Sports 3",
"http://lorempixel.com/50/50/sports/3",
];
var index, len;
for (index = 0, len = images.length; index < len; index++) {
var newoption = document.createElement('option');
newoption.innerHTML = images[index];
document.getElementById('imagelist').appendChild(newoption);
}
的HTML应该是这样的:
Sports 1
Sports 1
Sports 1
2016-03-01
DRB