js操作DOM在父元素中的結尾添加子節點注意

impressionHtml=`<img src=${value} alt=""/>`;
 document.getElementById("wrapper").appendChild(impressionHtml);

js向父元素wrapper中的末尾添加 定義好的html,報錯:

Uncaught TypeError: Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node'.

在stackoverflow上找到很好的一個解釋:

所以js是不能直接傳入字符串的,但是jquery的append可以直接傳入html字符串。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章