Monday, June 13, 2016

html - is content within an element its child node?



enter image description here



When watching the tutorial the teacher said that html was a child node of DOCUMENT, but said the text "jQuery Adventures" was "under" the title node.



So is the text within an element is technically a child node of that element or does it just appear under it in the DOM? Does it ever matter when coding?


Answer




Yes, a contiguous run of text inside an element becomes a text node which is a child of that element.





var el = document.querySelector('div');
console.log(el.childNodes.length); // 1 child node
console.log(el.childNodes[0].nodeType === Node.TEXT_NODE); // text node
console.log(el.childNodes[0].nodeValue); // text contents

Hello





No comments:

Post a Comment