Here's an HTML document
<body>
When we get the next node of the UL tag, we get used to quoting the following JSvar every = document.getElementById ("Everywhere"); and remove it from the document Console.info (Every.parentnode.childnodes[0].nodetype);
But we found that the console prints theNodetype:3. (Chorme Browser test)What is the reason for this problem?
This is because there is a space between the UL label and the LI Tag, the browser is mistaken for the text node , so print out the nodetype=3, and actually we need to get the element node Li
How to solve this problem?
The principle is simple, that is to remove the space between the nodes can be;
Here is a function to clear the space
function Cleanwhitespace (oeelement) {for (var i=0;i<oeelement.childnodes.length;i++) { var node= Oeelement.childnodes[i]; if (node.nodetype==3 &&!/\s/.test (node.nodevalue)) { node.parentNode.removeChild (node) } } }
By using this function, the console prints out thenodetype:1--"This is the positive solution."For detailed examples see the following index.html