js 获取 DOM 节点 值、类型、名称 的方法

<!-- 这是关于 input 的上一个注释 -->
<input type="password" id="ipt" name="password">
<!-- 这是关于  input 的 下一个 注释 -->

<script>
    // 获取元素的  节点名称  节点值   节点类型
    // 获取  html 元素
    var html = document.documentElement;
    console.log("节点名称:", html.nodeName, "节点值", html.nodeValue, "节点类型", html.nodeType);
    //节点名称: HTML 节点值 null 节点类型 1

    // 获取元素的属性节点 
    var iptAll = document.querySelector("#ipt");
    var ipt = iptAll.getAttributeNode("id");
    console.log("属性节点的名称", ipt.nodeName, "属性节点的值", ipt.nodeValue, "属性节点的类型", ipt.nodeType);
    //属性节点的名称 id 属性节点的值 ipt 属性节点的类型 2 


    // 获取元素的文本节点
    var txt = document.getElementsByTagName('title')[0].firstChild;
    console.log("属性节点的名称", txt.nodeName, "节点值", txt.nodeValue, "节点类型", txt.nodeType);
    //属性节点的名称 #text 节点值 标题 节点类型 3


    // 获取元素的注释节点  下一个注释
    var nextZS = iptAll.nextSibling.nextSibling;
    console.log("注释节点的名称", nextZS.nodeName, "注释节点的值", nextZS.nodeValue, "注释节点的类型", nextZS.nodeType);
    // 注释节点的名称 #comment 注释节点的值  这是一段文本的注释  注释节点的类型 8

    
    // 获取元素的注释节点  上一个节点
    var parentZS = iptAll.previousSibling.previousSibling;
    console.log("注释节点的名称", parentZS.nodeName, "注释节点的值", parentZS.nodeValue, "注释节点的类型", parentZS.nodeType);
    //注释节点的名称 #comment 注释节点的值  这是关于 input 的上一个注释  注释节点的类型 8


    // 获取文档的值 类型  名称
    console.log("文档名称", document.nodeName, "文档值", document.nodeValue, "节点类型", document.nodeType);
    //dom.html:21 文档名称 #document 文档值 null 节点类型 9


</script>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大闸蟹~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值