节点属性

nodeName

:::info node.nodeName返回节点的名字
该属性是「只读」属性
元素节点访问该属性返回的名字是大写的

字符串的方法:
str.toLowerCase() 字符串转小写
str.toUpperCase()字符串转大写 :::

  1. <div class="box" id="box" style="background-color: green">
  2. 我是文本节点
  3. <!--我是注释 -->
  4. <h1>我是h1</h1>
  5. <a href="">我是超链接</a>
  6. <p>我是段落标签</p>
  7. </div>
  1. console.log(document.nodeName); // #document
  2. console.log(document.getElementsByTagName("div")[0].nodeName); // DIV,元素节点返回的字符串为大写
  3. console.log(document.getElementsByTagName("div")[0].firstChild.nodeName); // #text
  4. console.log(document.getElementsByTagName("div")[0].childNodes[1].nodeName); // #commment
  5. console.log(document.getElementsByTagName("div")[0].childNodes[3].nodeName); // H1

nodeValue

:::info node.nodeValue返回节点的值
该属性是「只读」属性
只有文本节点、注释节点、属性节点才有该属性 :::

  1. console.log(document.getElementsByTagName("div")[0].nodeValue); // null
  2. console.log(document.getElementsByTagName("div")[0].firstChild.nodeValue); // " 我是文本节点 "
  3. console.log(document.getElementsByTagName("div")[0].childNodes[1].nodeValue); // 我是注释
  4. console.log(document.getElementsByTagName("div")[0].getAttributeNode("id").nodeValue); // box,getAttributeNode 获取属性节点(了解即可)

nodeType

:::info node.nodeType返回节点的类型值
该属性是「只读」属性

之前学习过节点的类型总共包括:

  • DOM元素节点,代表码1
  • 属性节点,代表码2
  • 文本节点,代表码3
  • 注释节点,代表码8
  • document节点,代表码9
  • documentFragment节点,代表码11 :::
    1. console.log(document.getElementsByTagName("div")[0].nodeType); // 1
    2. console.log(document.getElementsByTagName("div")[0].firstChild.nodeType); // 3
    3. console.log(document.getElementsByTagName("div")[0].childNodes[1].nodeType); // 8

因为childNodes返回是节点类型的集合,里面包含了所有的节点类型,我们可以利用nodeType封装一个方法只获取「元素节点」。

  1. function elementChildren(node) {
  2. var arr = [];
  3. var children = node.childNodes;
  4. for (var i = 0; i < children.length; i++) {
  5. if (children[i].nodeType === 1) {
  6. arr.push(children[i]);
  7. }
  8. }
  9. return arr;
  10. }
  11. let res = elementChildren(document.getElementsByTagName("div")[0]);
  12. console.log(res); // [h1, a, p]

attributes

:::info 返回节点的属性集合
该属性「可读可写」 :::

  1. console.log(document.getElementsByTagName("div")[0].attributes); // NamedNodeMap {0: class, 1: id, 2: style, class: class, id: id, style: style, length: 3}
  2. console.log(document.getElementsByTagName("div")[0].attributes[0]); // class=“box”
  3. console.log(document.getElementsByTagName("div")[0].attributes[0].nodeValue); // box

tagName

:::info 返回元素节点的大写标签名 :::

  1. document.getElementsByTagName("div")[0].tagName; // DIV

DOM 结构

WX20220523-111419.png
DOM结构树分为多种对象,它们之间是一种原型链的继承关系。
TODO!!!

  1. ## document 对象
  2. document.__proto__ => HTMLDocument.prototype
  3. HTMLDocument.prototype.__proto__ => Document.prototype
  4. Document.prototype.__proto__ => Node.prototype
  5. 所以 getElementByIdgetElementsByTagName 等方法都继承 Document.prototype
  6. ## CharacterData 对象
  7. Text.prototype.__proto__ => CharacterData.prototype
  8. Comment.prototype.__proto__ => CharacterData.prototype
  9. ## Element
  10. HTMLElement.prototype.__proto_ => Element.prototype
  11. XMLElement.prototype.__proto_ => Element.prototype

获取全部标签

document.getElementsByTagName()方法可以接受通配符*来获取文档全部的标签。

  1. console.log(document.getElementsByTagName("*"));
  2. // HTMLCollection(13) [html, head, meta, meta, meta, title, body, span, div#box.box, h1, a, p, script, viewport: meta, box: div#box.box]

获取文档信息

获取文档的head

  1. console.log(document.head);

获取文档的body

  1. console.log(document.body);

获取文档的title

  1. console.log(document.title);

获取文档的html

  1. console.log(document.documentElement);