我发现自己使用JavaScript和我运行的childNodes和儿童属性。我想知道它们之间的区别是什么。还有,两者孰优孰劣?


当前回答

到目前为止,答案不错,我只想补充一点,你可以使用nodeType检查节点的类型:

yourElement.nodeType

这将给你一个整数:(从这里取)

| Value |             Constant             |                          Description                          |  |
|-------|----------------------------------|---------------------------------------------------------------|--|
|    1  | Node.ELEMENT_NODE                | An Element node such as <p> or <div>.                         |  |
|    2  | Node.ATTRIBUTE_NODE              | An Attribute of an Element. The element attributes            |  |
|       |                                  | are no longer implementing the Node interface in              |  |
|       |                                  | DOM4 specification.                                           |  |
|    3  | Node.TEXT_NODE                   | The actual Text of Element or Attr.                           |  |
|    4  | Node.CDATA_SECTION_NODE          | A CDATASection.                                               |  |
|    5  | Node.ENTITY_REFERENCE_NODE       | An XML Entity Reference node. Removed in DOM4 specification.  |  |
|    6  | Node.ENTITY_NODE                 | An XML <!ENTITY ...> node. Removed in DOM4 specification.     |  |
|    7  | Node.PROCESSING_INSTRUCTION_NODE | A ProcessingInstruction of an XML document                    |  |
|       |                                  | such as <?xml-stylesheet ... ?> declaration.                  |  |
|    8  | Node.COMMENT_NODE                | A Comment node.                                               |  |
|    9  | Node.DOCUMENT_NODE               | A Document node.                                              |  |
|   10  | Node.DOCUMENT_TYPE_NODE          | A DocumentType node e.g. <!DOCTYPE html> for HTML5 documents. |  |
|   11  | Node.DOCUMENT_FRAGMENT_NODE      | A DocumentFragment node.                                      |  |
|   12  | Node.NOTATION_NODE               | An XML <!NOTATION ...> node. Removed in DOM4 specification.   |  |

请注意,根据Mozilla:

以下常量已弃用,不应使用 了:节点。ATTRIBUTE_NODE,节点。ENTITY_REFERENCE_NODE,节点。ENTITY_NODE,节点。NOTATION_NODE

其他回答

到目前为止,答案不错,我只想补充一点,你可以使用nodeType检查节点的类型:

yourElement.nodeType

这将给你一个整数:(从这里取)

| Value |             Constant             |                          Description                          |  |
|-------|----------------------------------|---------------------------------------------------------------|--|
|    1  | Node.ELEMENT_NODE                | An Element node such as <p> or <div>.                         |  |
|    2  | Node.ATTRIBUTE_NODE              | An Attribute of an Element. The element attributes            |  |
|       |                                  | are no longer implementing the Node interface in              |  |
|       |                                  | DOM4 specification.                                           |  |
|    3  | Node.TEXT_NODE                   | The actual Text of Element or Attr.                           |  |
|    4  | Node.CDATA_SECTION_NODE          | A CDATASection.                                               |  |
|    5  | Node.ENTITY_REFERENCE_NODE       | An XML Entity Reference node. Removed in DOM4 specification.  |  |
|    6  | Node.ENTITY_NODE                 | An XML <!ENTITY ...> node. Removed in DOM4 specification.     |  |
|    7  | Node.PROCESSING_INSTRUCTION_NODE | A ProcessingInstruction of an XML document                    |  |
|       |                                  | such as <?xml-stylesheet ... ?> declaration.                  |  |
|    8  | Node.COMMENT_NODE                | A Comment node.                                               |  |
|    9  | Node.DOCUMENT_NODE               | A Document node.                                              |  |
|   10  | Node.DOCUMENT_TYPE_NODE          | A DocumentType node e.g. <!DOCTYPE html> for HTML5 documents. |  |
|   11  | Node.DOCUMENT_FRAGMENT_NODE      | A DocumentFragment node.                                      |  |
|   12  | Node.NOTATION_NODE               | An XML <!NOTATION ...> node. Removed in DOM4 specification.   |  |

请注意,根据Mozilla:

以下常量已弃用,不应使用 了:节点。ATTRIBUTE_NODE,节点。ENTITY_REFERENCE_NODE,节点。ENTITY_NODE,节点。NOTATION_NODE

元素。children只返回子元素,而Node. children只返回子元素。childNodes返回所有子节点。注意,元素是节点,所以两者都可以在元素上使用。

我认为childNodes更可靠。例如,MDC(上面的链接)指出IE只有在ie9中才有子程序。childNodes为浏览器实现者提供了更少的出错空间。

理解.children是一个元素的属性。1只有元素有.children,这些子元素都是Element类型

然而,. childnodes是Node的一个属性,. childnodes可以包含任何节点。3.

一个具体的例子是:

let el = document.createElement("div");
el.textContent = "foo";

el.childNodes.length === 1; // Contains a Text node child.
el.children.length === 0;   // No Element children.

大多数情况下,您希望使用.children,因为通常不希望在DOM操作中遍历Text或Comment节点。

如果您确实希望操作Text节点,则可能需要使用. textcontent。4


1. 从技术上讲,它是ParentNode的一个属性,是Element包含的一个mixin。 2. 它们都是元素,因为.children是一个HTMLCollection,它只能包含元素。 3.类似地,. childnodes可以保存任何节点,因为它是一个NodeList。 4. 或.innerText。看这里和这里的区别。