JavaScript solves white-space node problems in the DOM

Source: Internet
Author: User

Here's an HTML document


<body>    

When we get the next node of the UL tag, we get used to quoting the following JS

var 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









Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.