zoukankan      html  css  js  c++  java
  • 妙味——文档碎片

    document.createDocumentFragment()

    1、普通创建方法:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
    <head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <title>Document</title>
    <script>
    window.onload=function(){
        var oBtn = document.getElementById('btn');
        var oUl = document.getElementById('ul1');
        
        oBtn.onclick=function(){
            var iStart = new Date().getTime();
            var i = 0;
    
            for(i=0;i<10000;i++){
                var oLi = document.createElement('li');
    
                oUl.appendChild(oLi);
            }
    
            alert(new Date().getTime()-iStart);
        };
    }
    </script>
    </head>
    <body>
        <input type="button" value="普通方式" id="btn" />
        <ul id="ul1"></ul>
    </body>
    </html>

    2、文档碎片方法:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
    <head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <title>Document</title>
    <script>
    window.onload=function(){
        var oBtn = document.getElementById('btn');
        var oUl = document.getElementById('ul1');
        
        oBtn.onclick=function(){
            var iStart = new Date().getTime();
            var oFrag = document.createDocumentFragment();    //创建文档碎片用
            var i = 0;
    
            for(i=0;i<10000;i++){
                var oLi = document.createElement('li');
    
                oFrag.appendChild(oLi);
            }
    
            oUl.appendChild(oFrag);
    
            alert(new Date().getTime()-iStart);
        };
    }
    </script>
    </head>
    <body>
        <input type="button" value="碎片方式" id="btn" />
        <ul id="ul1"></ul>
    </body>
    </html>

    有时候文档碎片方法反而会影响性能,多用于面试。

    高否?富否?帅否? 否? 滚去学习!
  • 相关阅读:
    洛谷 4035 [JSOI2008]球形空间产生器
    洛谷 2216 [HAOI2007]理想的正方形
    洛谷2704 [NOI2001]炮兵阵地
    洛谷2783 有机化学之神偶尔会做作弊
    洛谷 2233 [HNOI2002]公交车路线
    洛谷2300 合并神犇
    洛谷 1641 [SCOI2010]生成字符串
    Vue history模式支持ie9
    VUE实现登录然后跳转到原来的页面
    vue history模式 apache配置
  • 原文地址:https://www.cnblogs.com/baixc/p/3458288.html
Copyright © 2011-2022 走看看