appendChild + createElement

后端 未结 6 785
轻奢々
轻奢々 2020-12-09 09:54

What\'s the difference between:

var div = document.createElement(\'div\');//output -> [object HTMLDivElement]

document.getElementById(\'container\').appe         


        
6条回答
  •  心在旅途
    2020-12-09 10:30

    appendChild really does expect a HTMLDomNode of some kind, such as a HTMLDivElement, and not a string. It doesn't know how to deal with a string. You could do

    document.getElementById('container').innerHTML += div;
    

    but I really prefer the first version; and I'd rely more on it to behave the same across browsers.

提交回复
热议问题