zoukankan      html  css  js  c++  java
  • dom操作

    DOM是为文档对象模型

    dom操作主要是指对象中的节点(元素,文本,注释。。。等)。

    childNodes 需要用nodeType兼容
    nodeType = 1: 元素节点
    nodeType = 3: 文本节点

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</title>
        <script>
            window.onload = function(){
                var oUl = document.getElementsByTagName( "ul" )[0];
                //childNodes: 某个元素节点的 子节点集合
                var aChild = oUl.childNodes;
                // alert( aChild.length );
                //每一个节点都有一个类型,可以用nodeType来检查
                // nodeType = 3 文本节点  nodeType = 1 元素节点(标签)
                for( var i = 0; i < aChild.length; i++ ){
                    // alert( aChild[i].nodeType );
                    if( aChild[i].nodeType == 1 )
                        aChild[i].style.backgroundColor = 'red';
                }
            }
        </script>
    </head>
    <body>
        <ul>
            <li>1</li>
            <li>2</li>
            <li>3</li>
        </ul>
    </body>
    </html>

    children ( 子节点 集合 )

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</title>
        <script>
            window.onload = function(){
                //children: 获取当前节点下的所有元素子节点
                var oUl = document.getElementsByTagName( "ul" )[0];
                var aChild = oUl.children;
                for( var i = 0; i < aChild.length; i++ ){
                    aChild[i].style.backgroundColor = 'red';
                }
            }
        </script>
    </head>
    
    <body>
        <ul>
            <li>1</li>
            <li>2</li>
            <li>3</li>
        </ul>
    </body>
    
    </html>


    parentNode( 父节点 )

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</title>
        <script>
            window.onload = function(){
                var aLi = document.querySelectorAll( "li" ),
                    aHref = document.querySelectorAll( 'a' );
                for( var i = 0 ; i < aHref.length; i++ ){
                    aHref[i].onclick = function(){
                        this.parentNode.style.display = 'none';
                    }
                }
            }
        </script>
    </head>
    <body>
        <ul>
            <li>1<a href="javascript:;">隐藏</a></li>
            <li>2<a href="javascript:;">隐藏</a></li>
            <li>3<a href="javascript:;">隐藏</a></li>
        </ul>
    </body>
    </html>

    chrome,ff:
    previousElementSibling, nextElementSibling
    firstElementChild, lastElementChild
    ie8:
    previousSibling, nextSibling
    firstChild, lastChild

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</title>
        <style>
            div {
                padding:10px;
                margin:10px;
                border:1px solid #ccc;
            }
        </style>
        <script>
            window.onload = function(){
                var oBtn = document.querySelector( "input" ),
                    aDiv = document.querySelectorAll( 'div' );
                oBtn.onclick = function(){
                    // alert(  document.body.firstChild.value || document.body.firstElementChild.value );
                    // alert(  document.body.lastChild.innerHTML || document.body.lastElementChild.innerHTML );
                    //previous: 上一个元素
                    // alert(  aDiv[2].previousSibling.innerHTML || aDiv[2].previousElementSibling.innerHTML );
                    alert(  aDiv[2].nextSibling.innerHTML || aDiv[2].nextElementSibling.innerHTML );
                }
            }
        </script>
    </head>
    <body>
       <input type="button" value="点我"> 
       <div>this is div1</div>
       <div>this is div2</div>
       <div>this is div3</div>
       <div>this is div4</div>
       <div>this is div5</div>
    </body>
    </html>

    appendChild( 每次往父元素的最后面 追加节点 )

    insertBefore: 要做ie8兼容

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</title>
        <script>
            window.onload = function(){
                var oUl = document.querySelector( "#box" ),
                    oTxt = document.querySelector( "input" ),
                    oBtn = document.querySelector( 'input[type="button"]' ),
                    // aLi = document.getElementsByTagName( "li" );
                    aLi = document.getElementsByTagName( "li" );
    
                oBtn.onclick = function(){
                    var oLi = document.createElement( 'li' );
                    oLi.innerHTML = oTxt.value;
                    // oUl.insertBefore( oLi, aLi[0] );
                    if( aLi[0] ){
                        oUl.insertBefore( oLi, aLi[0] );
                    }else {
                        oUl.appendChild( oLi );
                    }
                }
            }
        </script>
    </head>
    <body>
       <input type="text" name="" id=""> 
       <input type="button" value="追加">
       <ul id="box"></ul>
    </body>
    </html>

    cloneNode( 深拷贝:true 浅拷贝:false )

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</title>
        <style>
            ul{
                border:1px solid red;
                padding:5px;
            }
        </style>
        <script>
            window.onload = function(){
                var oBtn1 = document.querySelector( '#btn1' ),
                    oBtn2 = document.querySelector( '#btn2' ),
                    oUl = document.querySelector( "ul" );
                oBtn1.onclick = function(){
                   document.body.appendChild( oUl.cloneNode( true ) );
                }
                oBtn2.onclick = function(){
                   document.body.appendChild( oUl.cloneNode( false ) );
                }
            }
        </script>
    </head>
    
    <body>
        <input type="button" value="深拷贝" id="btn1">
        <input type="button" value="浅拷贝" id="btn2">
        <ul onclick="alert(this.innerHTML);">
            <li>1</li>
            <li>2</li>
            <li>3</li>
            <li>4</li>
        </ul>
    </body>
    
    </html>

    replaceChild

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</title>
        <script>
            window.onload = function(){
                var oBtn = document.querySelector( "input" ),
                    aSpan = document.querySelectorAll( "span" );
                oBtn.onclick = function(){
                    for( var i = 0; i < aSpan.length; i++ ){
                        var oDiv = document.createElement( "div" );
                        oDiv.innerHTML = aSpan[i].innerHTML;
                        document.body.replaceChild( oDiv, aSpan[i] );
                    }
                }
            }
        </script>
    </head>
    <body>
       <input type="button" value="替换"> 
       <span>this is test string</span>
       <span>this is test string</span>
    </body>
    </html>

    removeChild

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</title>
        <script>
            window.onload = function(){
                var aHref = document.querySelectorAll( "a" ),
                    oUl = document.querySelector( "ul" );
                for( var i = 0; i < aHref.length; i++ ){
                    aHref[i].onclick = function(){
                        //父节点.removeChild( 子节点 )
                        oUl.removeChild( this.parentNode );
                    }
                }
            }
        </script>
    </head>
    <body>
        <ul>
            <li>1<a href="javascript:;">移除1</a></li>
            <li>2<a href="javascript:;">移除2</a></li>
            <li>3<a href="javascript:;">移除3</a></li>
        </ul>
    </body>
    </html>
  • 相关阅读:
    【HTML5 绘图与动画】使用canvas
    【H5新增元素和文档结构】新的全局属性 1. contentEditable 可编辑内容 2. contextmenu 快捷菜单 3. data 自定义属性 4. draggable 可拖动 5. dropzone 拖动数据 6. hidden 隐藏 7. spellcheck 语法检查 8. translate 可翻译
    【H5新增元素和文档结构】完善旧元素 1. a 超链接 2. ol 有序列表 3. dl 定义列表 4. cite 引用文本 5. small 小号字体 6. iframe 浮动框架 7. script 脚本
    【H5新增元素和文档结构】新的语义信息 1. address 2. time 3. figure 跟 figcaption 4. details 和 summary 5. mark 6. progress 7. meter 8. dialog 9.bdi 10. wbr 11. ruby、rt、rp 12. command
    【H5新增元素跟文档结构】新的文档结构 1. article 文章块 2. section 区块 3. nav 导航条 4. aside 辅助栏 5. main 主要区域 6. header 标题栏 7. hgroup 标题组 8. footer 页脚栏
    5_PHP数组_3_数组处理函数及其应用_9_数组集合运算函数
    【华为云技术分享】鲲鹏弹性云服务器GCC交叉编译环境搭建指南
    【华为云技术分享】7 分钟全面了解位运算
    【华为云技术分享】Linux内核编程环境 (1)
    【华为云技术分享】华为云MySQL 8.0正式商用,全新增强版开源利器强势来袭
  • 原文地址:https://www.cnblogs.com/momomiji/p/7598799.html
Copyright © 2011-2022 走看看