• nodeType


    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>node Type</title>
    </head>
    <body>
    <!--nodeType, nodeName, nodeValue实验-->
    <div id="container">这是一个元素节点</div>

    <script>
    var divNode = document.getElementById('container');

    //获取元素节点
    console.log(divNode.nodeName + '/' + divNode.nodeValue);
    if (divNode.nodeType == 1){
    console.log('Node is an element.');
    }
    /*if (divNode.nodeType == Node.ELEMENT_NODE){
    console.log('Node is an element.');
    }*/

    //获取属性节点 attributes保存了元素的所有属性
    var attrNode = divNode.attributes[0];
    console.log(attrNode.nodeName + '/' + attrNode.nodeValue);
    if (attrNode.nodeType == 2){
    console.log('Node is an attr.')
    }
    /*if (attrNode.nodeType == Node.ATTRIBUTE_NODE){
    console.log('Node is an attr.');
    }*/

    //获取文本节点 childNodes获取父元素所有的子元素
    var textNode = divNode.childNodes[0];
    console.log(textNode.nodeName + '/' + textNode.nodeValue);
    if (textNode.nodeType == 3){
    console.log('Node is a text.')
    }
    /*if (attrNode.nodeType == Node.TEXT_NODE){
    console.log('Node is a text.');
    }*/

    //获取注释节点 body第一个子元素是空白节点,注释节点是第二个
    var commentNode = document.body.childNodes[1];
    console.log(commentNode.nodeName + '/' + commentNode.nodeValue);
    if (commentNode.nodeType == 8){
    console.log('Node is a comment.')
    }
    /*if (commentNode.nodeType == Node.COMMENT_NODE){
    console.log('Node is a comment.');
    }*/

    //获取文档类型节点,直接写到控制台
    console.log(document.doctype.nodeName + '/' + document.doctype.nodeValue);
    if (document.doctype.nodeType == 10){
    console.log('Node is a DoctypeText.')
    }
    /*if (document.doctype.nodeType == Node.DOCUMENT_TYPE_NODE){
    console.log('Node is a DoctypeText.');
    }*/

    //获取文档片段节点
    var frag = document.createDocumentFragment();
    console.log(frag.nodeName + '/' + frag.nodeValue);
    if (frag.nodeType == 11){
    console.log('Node is a DocumentFragment.')
    }
    /*if (commentNode.nodeType == Node.COMMENT_FRAGMENT_NODE){
    console.log('Node is a DocumentFragment.');
    }*/
    </script>
    </body>
    </html>

  • 相关阅读:
    攻防一体 暴力攻击
    新的亮眼的但不彻底的交互
    利用物联网或智能化区分产品
    Character Sets, Collation, Unicode :: utf8_unicode_ci vs utf8_general_ci
    容灾 RPO RTO
    微信找人代付 下单账号 支付账号
    微信公众号 openId 支付 php中file_get_contents与curl性能比较分析
    t
    accesstoken 中控服务器 并发刷新 加并发锁
    a
  • 原文地址:https://www.cnblogs.com/JCBX1688/p/6663507.html
Copyright © 2020-2023  润新知