在 javascript 中打印一组图像
Issue printing an array of images in javascript
我正在使用图像数组制作一个简单的图库:
var gallery_pictures = [
{ name:'01.jpg', alt:'01'},
{ name:'02.jpg', alt:'01'},
{ name:'03.jpg', alt:'01'},
{ name:'04.jpg', alt:'01'},
{ name:'05.jpg', alt:'01'}
];
在此容器内:
<div style="width:1000px; margin: 0 auto;" id="gallery">
</div>
并使用将文本存储在使用映射的变量中的函数:
var gallery = gallery_pictures.map(function gallery(foto, index, array) {
pictures = '<div style="width:190px;margin-right: 10px; float:left;"><img width="200" src="'+foto.name+'" alt="'+foto.alt+'" "></div>'
return pictures;
});
然后我使用 innerHTML 在我的 div 中打印它:
imageGallery.innerHTML=gallery;
我遇到的问题是每张图片之间以某种方式打印了一个逗号“,”。这里的代码结果为 chrome:
这是它在屏幕上的样子:
你要的是each或者普通的for循环。
您已将数组打印为 html。这就是它们看起来像数组元素的原因。
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/map
The map() method creates a new array with the results of calling a provided function on every element in the calling array.
您正在取回一个包含其中元素的数组。这就是您看到逗号的原因。
您需要以其中没有逗号的方式连接数组。
var gallery = "";
gallery_pictures.forEach(function(foto){
var pictures = '<div style="width:190px;margin-right: 10px; float:left;"><img width="200" src="'+foto.name+'" alt="'+foto.alt+'" "></div>';
gallery += pictures;
});
imageGallery.innerHTML = gallery;
我正在使用图像数组制作一个简单的图库:
var gallery_pictures = [
{ name:'01.jpg', alt:'01'},
{ name:'02.jpg', alt:'01'},
{ name:'03.jpg', alt:'01'},
{ name:'04.jpg', alt:'01'},
{ name:'05.jpg', alt:'01'}
];
在此容器内:
<div style="width:1000px; margin: 0 auto;" id="gallery">
</div>
并使用将文本存储在使用映射的变量中的函数:
var gallery = gallery_pictures.map(function gallery(foto, index, array) {
pictures = '<div style="width:190px;margin-right: 10px; float:left;"><img width="200" src="'+foto.name+'" alt="'+foto.alt+'" "></div>'
return pictures;
});
然后我使用 innerHTML 在我的 div 中打印它:
imageGallery.innerHTML=gallery;
我遇到的问题是每张图片之间以某种方式打印了一个逗号“,”。这里的代码结果为 chrome:
这是它在屏幕上的样子:
你要的是each或者普通的for循环。
您已将数组打印为 html。这就是它们看起来像数组元素的原因。
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/map
The map() method creates a new array with the results of calling a provided function on every element in the calling array.
您正在取回一个包含其中元素的数组。这就是您看到逗号的原因。
您需要以其中没有逗号的方式连接数组。
var gallery = "";
gallery_pictures.forEach(function(foto){
var pictures = '<div style="width:190px;margin-right: 10px; float:left;"><img width="200" src="'+foto.name+'" alt="'+foto.alt+'" "></div>';
gallery += pictures;
});
imageGallery.innerHTML = gallery;