appendChild + createElement [英] appendChild + createElement

查看:151
本文介绍了appendChild + createElement的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么区别:

What's the difference between:

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

document.getElementById('container').appendChild(div);



and:

var div = '<div></div>';

document.getElementById('container').appendChild(div);//output -> <div></div>

不应该两个都一样吗?如果不是,我该如何获得第二个版本?

Shouldn't both be the same? And if not, how do I get the second version to work?

推荐答案

物体。首先,你需要 appendChild ,而后者需要 innerHTML

The later is pure html for an element while first is the object. For the first, you need appendChild while for the later, you need innerHTML.


不应该一样吗?如果
不是,我如何获得第二个版本到
工作?

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



var div = '<div></div>';
document.getElementById('container').innerHTML = div;

这篇关于appendChild + createElement的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆