截至目前,您的循环仅迭代一次,因为您return
在其中有语句,该语句返回函数的值。您需要做的是存储结果,然后最后返回它们。
function grindTeen(teenused) {
var images = [];
var arrayItem = null;
for (var i = 0; i < teenused.length; i++) {
arrayItem = teenused[i];
images.push('<img src="/bena/design/icons/B-icons-'+arrayItem+'.png" width="33" height="33">');
}
return images;
};
var teenImages = grindTeen(teenused);
// take the items in the teenImages list and make it into a string separated by space
var teenImagesHtml = teenImages.join(' ');
您可以阅读更多关于.join()
https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/join
这是工作小提琴:http: //jsfiddle.net/amyamy86/V4Rwh/
更新:
对于数组['01', '02', '03', '04', '05', '06']
,输出将是(如果放入<div>
):
<div id="c">
<img src="/bena/design/icons/B-icons-01.png" width="33" height="33">
<img src="/bena/design/icons/B-icons-02.png" width="33" height="33">
<img src="/bena/design/icons/B-icons-03.png" width="33" height="33">
<img src="/bena/design/icons/B-icons-04.png" width="33" height="33">
<img src="/bena/design/icons/B-icons-05.png" width="33" height="33">
<img src="/bena/design/icons/B-icons-06.png" width="33" height="33">
</div>