Proficient in JavaScript to correct cleanWhitespace Functions

Source: Internet
Author: User

Copy codeThe Code is as follows:
Function cleanWhitespace (element ){
// Process the entire HTML document if no parameter is provided
Element = element | document;
// Use the first subnode as the start pointer
Var cur = element. firstChild;
// Temporary variables are used to save the next node of the current node
Var tmp;
// Until no subnode exists
While (cur! = Null ){
// Save the next node of the current node
Tmp = cur. nextSibling
// If the node is a text node, it should contain spaces
If (cur. nodeType = 3 &&! /\ S/. test (cur. nodeValue )){
// Delete the text node
Element. removeChild (cur );
// Otherwise, it is an element
} Else if (cur. nodeType = 1 ){
// Recursive entire document
CleanWhitespace (cur );
}
Cur = tmp; // traverse sub-nodes
}
}

Another usable
Copy codeThe Code is as follows:
Function cleanWhitespace2 (node ){
Var notWhitespace =/\ S /;
For (var I = 0; I <node. childNodes. length; I ++ ){
Var childNode = node. childNodes [I];
If (childNode. nodeType = 3 )&&(! NotWhitespace. test (childNode. nodeValue ))){
Node. removeChild (node. childNodes [I]);
I --;
}
If (childNode. nodeType = 1 ){
CleanWhitespace2 (childNode );
}
}
}

If only the blank space of the current node is cleared, the child node is not traversed.
Copy codeThe Code is as follows:
Function cleanWhitespace2 (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 )}
}
}

Another usable
Copy codeThe Code is as follows:
Function cleanWhitespace2 (node ){
Var notWhitespace =/\ S /;
For (var I = 0; I <node. childNodes. length; I ++ ){
Var childNode = node. childNodes [I];
If (childNode. nodeType = 3 )&&(! NotWhitespace. test (childNode. nodeValue ))){
Node. removeChild (node. childNodes [I]);
I --;
}
If (childNode. nodeType = 1 ){
CleanWhitespace2 (childNode );
}
}
}

If only the blank space of the current node is cleared, the child node is not traversed.
Copy codeThe Code is as follows:
Function cleanWhitespace2 (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 )}
}
}

Related Article

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.