我目前正在从事一个业余项目,并致力于 DOM 操作。
下面,我创建了一个函数addImage
。当浏览器调用该函数时,会返回以下错误:
TypeError: Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node'.
function addImage(data) {
let imageElement = new Image();
imageElement.src = data;
console.log(imageElement);
let imageContainer = document.getElementById('image-container');
imageContainer.appendChild('imageElement');
}
imageElement = `<img src="https://igcdn-photos-f-a.akamaihd.net/hphotos-ak-xpt1/t51.2885-19/11193017_906607852718693_821675199_a.jpg">`
我查看了其他带有此类型错误的问题。在这些示例中,字符串被添加到 div 元素中。
最佳答案
您使用了Node.appendChild()要将 imageElement
附加到 imageContainer
,您应该将一个元素 (node) 传递给 appendChild()
,但是您传递了字符串
,这就是错误的原因。
所以,改变
imageContainer.appendChild('imageElement');
至
imageContainer.appendChild(imageElement);
你就可以开始了。
关于javascript - 当appendChild(img)时参数1不是 'Node'类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44290163/