var parent = document.getElementById('parent');
for(var i = 0; i < 10000; i++) {
var child = document.createElement('div');
var text = document.createTextNode('' + i);
child.appendChild(text);
parent.appendChild(child);
}
var i = 0, max = 10000;
setTimeout(function addNodes() {
for(var step = i + 500; i < step; i++) {
var child = document.createElement('div');
child.appendChild(document.createTextNode('' + i));
div.appendChild(child);
}
if(i < max) {
setTimeout(addNodes, 0);
}
}, 0);
var parent = document.getElementById('parent');
var div = document.createElement('div');
for(var i = 0; i < 10000; i++) {
var child = document.createElement('div');
var text = document.createTextNode('' + i);
child.appendChild(text);
div.appendChild(child);
}
parent.appendChild(div);
var parent = document.getElementById('parent');
var frag = document.createDocumentFragment();
for(var i = 0; i < 10000; i++) {
var child = document.createElement('div');
var text = document.createTextNode('' + i);
child.appendChild(text);
frag.appendChild(child);
}
parent.appendChild(frag);
var div = document.createElement('div');
div.innerHTML = '<th>xxx</th>';
console.log(div);
<div>xxx</div>
<div><th>xxx</th></div>
var node = document.createElement('div');
node.innerHTML = '<table><tbody><tr><th>xxx</th></tr></tbody></table>';
// 把外面的几层皮扒掉就是想要的 th 了
var depth = 3;
while(depth--) {
node = node.lastChild;
}
console.log(node.firstChild);
// 需要单独处理的一些特殊节点
var map = {
legend : [1, '<fieldset>', '</fieldset>'],
tr : [2, '<table><tbody>', '</tbody></table>'],
col : [2, '<table><tbody></tbody><colgroup>', '</colgroup></table>'],
_default : [0, '', '']
};
map.td = map.th = [3, '<table><tbody><tr>', '</tr></tbody></table>'];
map.option = map.optgroup = [1, '<select multiple="multiple">', '</select>'];
map.thead = map.tbody = map.colgroup = map.caption = map.tfoot = [1, '<table>', '</table>']
map.text = map.circle = map.ellipse = map.line = map.path = map.polygon = map.polyline = map.rect = [1, '<svg xmlns="http://www.w3.org/2000/svg" version="1.1">','</svg>'];
var TAG_RE = /<([\w:]+)/;
module.exports = function(templateString) {
var frag = document.createDocumentFragment(),
m = TAG_RE.exec(templateString);
// 单纯字符串的情况
if(!m) {
frag.appendChild(document.createTextNode(templateString);
return frag;
}
var tag = m[1],
wrap = map[tag] || map._default,
depth = wrap[0],
prefix = wrap[1],
suffix = wrap[2],
node = document.createElement('div');
// 拼接节点字符串
node.innerHTML = prefix + templateString.trim() + suffix;
// 去除外包裹层,只留字符串转化的节点
while(depth--) node = node.lastChild;
// 只有一个节点的情况
if(node.firstChild === node.lastChild) {
frag.appendChild(node.firstChild);
return frag;
}
// 多个节点,依序添加到 frag
var child;
while(child = node.firstChild) {
frag.appendChild(child);
}
return frag;
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有