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 )}
}
}