• DOM操作


    1、在元素之前添加元素

    <html>
    <head>
    <meta charset="utf-8">
    <title>无标题文档</title>
    </head>
    
    <body>
    <input type="text" id="txt1" />
    <input type="button" id="btn1" value="创建" />
    <ul id="ul1"></ul>
    <script>
        var obtn=document.getElementById('btn1');
        var oul=document.getElementById('ul1');
        var otxt=document.getElementById('txt1');
        obtn.onclick=function()
        {
            var oli=document.createElement('li');  /*创建元素*/
            var ali=oul.getElementsByTagName('li');
            oli.innerHTML=otxt.value;
            if(ali.length>0)
                {
                    oul.insertBefore(oli,ali[0]);   /*在元素之前插入*/
                }
            else
                {
                    oul.appendChild(oli); 
                }
        }
    </script>
    </body>
    </html>

    2、在元素之后添加元素

    <html>
    <head>
    <meta charset="utf-8">
    <title>无标题文档</title>
    </head>
    
    <body>
    <input type="text" id="txt1" />
    <input type="button" id="btn1" value="创建" />
    <ul id="ul1"></ul>
    <script>
        var obtn=document.getElementById('btn1');
        var oul=document.getElementById('ul1');
        var otxt=document.getElementById('txt1');
        obtn.onclick=function()
        {
            var oli=document.createElement('li');  /*创建元素*/
            oli.innerHTML=otxt.value;
            oul.appendChild(oli);              /*把li作为一个子节点添加给父级ul*/
        }
    </script>
    </body>
    </html>

    3、删除元素

    <!doctype html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>无标题文档</title>
    </head>
    
    <body>
    <ul id="ul1">
        <li>blog.xinlvtian.com<a href="javascript:;">删除</a></li>
        <li>xinlvtian.com<a href="javascript:;">删除</a></li>
        <li>wwwxinlvtian.com<a href="javascript:;">删除</a></li>
    </ul>
    <script>
        var oa=document.getElementsByTagName('a');
        var oul=document.getElementById('ul1');
        for(var i=0;i<oa.length;i++)
            {
                oa[i].onclick=function()
                {
                    oul.removeChild(this.parentNode);    /*删除节点*/
                }
            }
    </script>
    </body>
    </html>

    4、文档碎片

    <html>
    <head>
    <meta charset="utf-8">
    <title>无标题文档</title>
    </head>
    
    <body>
    <ul id="ul1">
        
    </ul>
    <script>
        var oul=document.getElementById('ul1');
        var ofrag=document.createDocumentFragment();   /*创建文档碎片*/
        for(var i=0;i<10000;i++)   /*创建10000个li*/
            {
                var oli=document.createElement('li');
                ofrag.appendChild(oli);
                //oul.appendChild(oli);  /*没有创建文档碎片*/
            }
        oul.appendChild(ofrag);  /*文档碎片是针对低端的浏览器的,对于高端浏览器可能会降低性能*/
    </script>
    </body>
    </html>

    新绿天博客

  • 相关阅读:
    js原生实现div渐入渐出
    js刷新界面前事件onbeforeunload
    js手机短信验证
    scroll滚动条样式修改
    省市区三级联动
    js this的含义以及讲解
    炫酷实用的CSS3代码垂直手风琴菜单
    机器学习初探(手写数字识别)HOG图片
    机器学习初探(手写数字识别)matlab读取数据集
    Google B4网络阅读记录(翻译)
  • 原文地址:https://www.cnblogs.com/xinlvtian/p/7998872.html
Copyright © 2020-2023  润新知