<!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>节点名称nodeName</title>
    <!-- 
        node.nodeName;当node是元素时,获取的node节点名称是(标签)的名称,不带尖括号
        node.nodeName;当node是属性时,获取的node节点名称是属性的名称
        node.nodeName;当node是文本时,获取的node节点名称是字符串"#text"
     -->
</head>
<body>
    <p class="p1" style="background: red;">我是p节点的文本子节点</p>
    <script>
        var p=document.getElementsByTagName("p")[0];//获取p
        var text=p.firstChild;//获取p下面的第一个子节点
        var attribute=p.getAttribute("class");//获取p的class属性
        console.log(p.nodeName);//p的节点名
        console.log(text.nodeName);//p下面第一个子节点的名
        console.log(attribute.nodeName);//属性节点名
    </script>
</body>
</html>

相关文章:

  • 2022-12-23
  • 2021-10-26
  • 2021-04-27
  • 2021-06-23
  • 2021-06-21
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-08-27
  • 2022-03-10
  • 2022-12-23
  • 2021-10-22
  • 2022-03-04
  • 2021-12-14
  • 2022-12-23
相关资源
相似解决方案