The difference between childNodes.length and children.length_javascript skills
- WBOYOriginal
- 2016-05-16 18:53:101258browse
因为childNodes包含的不仅仅只有html节点,所有属性,文本等都包含在childNodes里面,你可以通过nodeType to determine which type of node it is, only when nodeType==1 is used as the element node, 2 is an attribute node, 3 is a text node. And children.length only counts the number of nodes.
Including calculation of node length, please pay attention to this when moving nodes.
To get the text of an element, for example, to get the text of a P node, there is People like to use innerHTML and the same goes for changes, which is actually not good because using innerHTMLThe browser will think that what you want to insert is a piece of tag text, and you only want to insert a piece of plain text, which will cause the browser to try to parse your piece of plain text, which will cause unnecessary resources waste.So it is recommended to use element.firstChild.nodeValue, Why is firstChild? Because childNodes[0] is a text node. If you don’t believe it, you can take a look at its nodeType .
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn