< html > <br><br> < head > <br> < title ></ title > <br> </ head > <br><br> < body > <br><br> < div > <br> < h1 > Header </ h1 > <br> < p > Paragraph </ p > <br> </ div > <br><br> < script type ="text/javascript" > <br> alert( document .getElementsByTagName( 'div' )[0].firstChild.tagName);<br> </ script > <br><br> </ body > <br><br> </ html > <br><br> * This source code was highlighted with Source Code Highlighter .
H1
element is the first child node ( node 'om) of the DIV
element. But it is not. If you follow the Document Object Model Core specification, line breaks, spaces, and other non-print characters are text nodes. Thus, in the above code, the string document.getElementsByTagName('div')[0].firstChild
returns a text node that does not have the tagName
property (since, in fact, it is not a tag).DIV
element will have not two child nodes, as it may first appear, but five.< div > <br> < h1 > Header </ h1 > <br> < p > Paragraph </ p > <br> </ div >
\n
", " H1
", " \n
", " P
", " \n
". Three of them are text nodes , two are element nodes .DIV
node will contain not five, but two child nodes.childNodes
property and make sure that IE does not count empty text nodes.Source: https://habr.com/ru/post/72773/
All Articles