怎么做网站弹出公告,郴州录取网站,php网站开发做什么,关于网站建设规划方书案例样式例子#xff1a;我们要实现的效果是当用户点击鼠标的时候#xff0c;就在旧数据上追加若干新数据。如果使用标准DOM的话#xff0c;完整代码如下#xff1a;testdatadocument.onmousedown function() {for (var i 0; i 10; i) {var p document.createElement(我们要实现的效果是当用户点击鼠标的时候就在旧数据上追加若干新数据。如果使用标准DOM的话完整代码如下testdatadocument.onmousedown function() {for (var i 0; i 10; i) {var p document.createElement(p);p.appendChild(document.createTextNode(Math.random()));document.getElementsByTagName(div)[0].appendChild(p);}};注一旦结构比较复杂的话标准DOM需要编写冗长的代码。如果使用innerHTML的话部分代码如下document.onmousedown function() {var html ;for (var i 0; i 10; i) {html Math.random() ;}document.getElementsByTagName(div)[0].innerHTML html;};注innerHTML没有标准DOM中的appendChild所以使用了『』的方式效率低下。我们可以结合使用innerHTML和标准DOM这样二者的优点就兼得了部分代码如下document.onmousedown function() {var html ;for (var i 0; i 10; i) {html Math.random() ;}var temp document.createElement(div);temp.innerHTML html;while (temp.firstChild) {document.getElementsByTagName(div)[0].appendChild(temp.firstChild);}};注创建一个元素然后注入innerHTML接着在元素上使用标准DOM操作。还不算完Asynchronous innerHTML给出了更强悍的解决方法部分代码如下document.onmousedown function() {var html ;for (var i 0; i 10; i) {html Math.random() ;}var temp document.createElement(div);temp.innerHTML html;var frag document.createDocumentFragment();(function() {if (temp.firstChild) {frag.appendChild(temp.firstChild);setTimeout(arguments.callee, 0);} else {document.getElementsByTagName(div)[0].appendChild(frag);}})();};注使用setTimeout防止堵塞浏览器使用DocumentFragment减少渲染次数。另代码在拼接字符串时还可以更快详见Fastest way to build an HTML string。