0
我目前正在处理几个包含AJAX调用的应用程序,并使用JSON格式从服务器检索数据。 每个页面都需要基于JSON响应来创建内容,而我目前正在创建喜欢的内容:动态创建元素
function createBox1(json) {
var bigbox = document.createElement('div');
bigbox.className = 'class1';
var firstbox = document.createElement('div');
firstbox.className = 'first-box';
var firstNestedBox = document.createElement('div');
var secondNestedBox = document.createElement('div');
var thirdNestedBox = document.createElement('div');
var secondbox = document.createElement('div');
...
所以基本上它有点长的代码,我想知道是否有更好的方法做它。
PS:我看到了一些图书馆,他们做这样的事情:
function o(t,e){var i=document.createElement(t||"div"),o;for(o in e)i[o]=e[o];return i}
,我想这就是他们创建多个div元素,但我不知道这是如何工作的。
在此先感谢(:
“使用碎片更快在构建标记时nt“是否意味着将所有内容插入到一个框中,然后将该框附加到DOM? – 2013-04-06 19:20:50
基本上是的。你创建一个[documentFragment](https://developer.mozilla.org/en-US/docs/DOM/document.createDocumentFragment)作为你的HTML的容器,当你创建它的时候,你插入一切一个去。由于DOM的变化非常昂贵并且需要时间,所以这是加快事情发展的更好方法之一。 – adeneo 2013-04-06 19:24:06