js添加DOM元素.txt var asideNav = document.getElementById('aside-nav'),//导航 oCons = document.getElementById('construction'),//内容 oh4 = oCons.querySelectorAll('h4'); frag = document.createDocumentFragment(),//Dom碎片 createObj =
var asideNav = document.getElementById('aside-nav'),//导航 oCons = document.getElementById('construction'),//内容 oh4 = oCons.querySelectorAll('h4'); frag = document.createDocumentFragment(),//Dom碎片 createObj = null,//创建元素 min = oCons.offsetTop, max = min + oCons.scrollHeight, aTop = document.getElementById('top'); for (var i = 0; i < oh4.length; i++) { createObj = document.createElement('a');//创建元素 createObj.innerText = oh4[i].innerText; oh4[i].id='title'+i; createObj.href = '#' + oh4[i].id; frag.appendChild(createObj);//先添加到Dom碎片中 } asideNav.appendChild(frag);一次性添加到DOM树下面渲染.