appendChild + createElement

后端 未结 6 772
轻奢々
轻奢々 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:29

    The latter is simply a string containing HTML while the first is an object. For the first, you need appendChild while for the second, you need to append to innerHTML.

    shouldn't both be the same? and if not, how do i get the 2nd version to work?

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

提交回复
热议问题