尝试将图像添加到列表中

我试图将图像源存储在一个数组中,然后使用javascript将它们添加到列表中。

这是我的代码:

var imgSrc = new Array ("images/hangings/1.jpg","images/hangings/2.jpg");
for (var i = 0; i < prices.length; i++)
{
    $('#products ul').append('<li><img src="imgSrc[0]"/></li>');
}

当我加载网站时,它显示我想要的图像框,但图像是空白的。 什么错了?/>

编辑。 我尝试使用单引号,但我得到一个“失踪”后参数列表“错误

采纳答案:

imgSrc需要作为函数调用,而不是作为字符串调用。

imgSrc[0]应该是imgSrc[i]以便它使用当前索引循环(每次不是0

$('#products ul').append('<li><img src="' + imgSrc[i] + '"/></li>');
author: curt

参考更多解答: Trying to add images to a list ,转载请保留出处尝试将图像添加到列表中及作者信息

Statement: We respect knowledge and authors. Since the content comes from the Internet and is intended for scientific research, any reprinters should retain the author's signature and origin. If you are the author of the content and feel in dispute, please contact email: 1076545519@qq.com. We will find out the situation and deal with it in time. We sincerely thank the author for his hard work.


更多:html