错误消息";无法读取属性';追加儿童';“无效”的定义;使用javascript createElement()将IMG追加到div中时

错误消息";无法读取属性';追加儿童';“无效”的定义;使用javascript createElement()将IMG追加到div中时,javascript,html,loops,dynamic,appendchild,Javascript,Html,Loops,Dynamic,Appendchild,这里是js的新成员。我想将每个图像标记(其中15个)附加到它自己的div标记中。但是,我在控制台中遇到了“无法读取属性'appendChild'为null”错误 function createImage() { const DIV_ID = ['b1', 'b2', 'b3', 'b4', 'b5', 'b6', 'b7', 'b8', 'b9', 'b10', 'b11', 'b12', 'b13', 'b14', 'b15']; img = []; cont

这里是js的新成员。我想将每个图像标记(其中15个)附加到它自己的div标记中。但是,我在控制台中遇到了“无法读取属性'appendChild'为null”错误

function createImage()
{
    const DIV_ID = ['b1', 'b2', 'b3', 'b4', 'b5', 'b6', 'b7', 'b8', 'b9', 'b10', 'b11', 'b12',
    'b13', 'b14', 'b15'];

    img = [];
    container = [];

    width = 120;
    height = 120;

    var src = "rdmImg.png";
    for (i = 0; i < 15; i++)
    {
        container[i] = document.createElement('div');
        container[i].setAttribute = ("id", `${DIV_ID[i]}`);
        document.body.appendChild(container[i]);

        img[i] = document.createElement('img'); 
        img[i].src = src;
        img[i].width = width;
        img[i].height = height;
        document.getElementById(`${DIV_ID[i]}`).appendChild(img[i]); //Cannot read property of null
    }
函数createImage()
{
const DIV_ID=['b1','b2','b3','b4','b5','b6','b7','b8','b9','b10','b11','b12',
‘b13’、‘b14’、‘b15’];
img=[];
容器=[];
宽度=120;
高度=120;
var src=“rdmImg.png”;
对于(i=0;i<15;i++)
{
容器[i]=document.createElement('div');
容器[i].setAttribute=('id',`${DIV_id[i]}');
document.body.appendChild(容器[i]);
img[i]=document.createElement('img');
img[i].src=src;
img[i].宽度=宽度;
img[i].高度=高度;
document.getElementById(`${DIV_ID[i]}`).appendChild(img[i]);//无法读取null的属性
}

任何一只手都非常感谢!

你有一个打字错误。它应该是
.setAttribute(“id”)、`${DIV\u id[i]};
container[i]。setAttribute=(“id”)、
${DIV\u id[i]}
;你有
=
但它应该是
container[i]。setAttribute(“id”)、
${DIV id[i]};