Node.src = variablename/添加InnerHTML和变量问题的HTML图像



我正在尝试通过将它们作为变量保存在数组中,然后编辑一些divs的innerhtml来包含它们,以将它们保存为"动态",但这些图像未显示。我正在使用img.src = varname;这是指代码的链接:https://repl.it/gboa/1(这也是上传图像的网站版本的链接:cardTestwdi.bitballoon.com)。我将感谢我能得到的任何帮助。

如果要使用字符串设置innerHTML,则必须提供完整的HTML字符串。您不能创建字符串,然后将属性用于常规DOM节点。例如,您可以使用以下内容:

var imgElement = "<img src='" + myArray[i] + "' alt='test' width='200px' height='275px'/>";

但是,我建议使用document.createElement在JavaScript中创建节点,然后使用document.appendChild

将其附加到DOM
var imgElement = document.createElement('img');
imgElement.width = 200;
imgElement.height = 275;
imgElement.src = myArray[i];
myCard[i].appendChild(imgElement); // Append the image node to the card node

最新更新