显然,我缺少了一些在createTextNode和createElement工作方式上非常明显的东西--我让一个非常简单的测试在div中显示“Hello”,但它没有显示:
var sp = document.createElement('div');
var tx = document.createTextNode('hello world');
sp.appendChild(tx);我在做什么蠢事?!
发布于 2013-12-28 18:58:48
你忘了附上sp。
document.getElementsByTagName('body')[0].appendChild(sp)演示
发布于 2013-12-28 19:07:35
TIP
如果计划添加大量元素,请缓存文档。阅读有关document.createDocumentFragment();的文章;
齐头并进
var d=window.document;
d.body.appendChild(d.createElement('div')).appendChild(d.createTextNode('hey'))演示
http://jsfiddle.net/Ba5VK/
发布于 2013-12-28 18:58:00
你忘了把div附加到文档上了。
https://stackoverflow.com/questions/20818156
复制相似问题