CopyCode The 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 usableCopy 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 usableCopy 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 current node is cleared, the child node copy Code the code is as follows: function cleanwhitespace2 (oeelement)
{< br> for (VAR I = 0; I var node = oeelement. childnodes [I];
If (node. nodetype = 3 &&! /\ S/. Test (node. nodevalue) {node. parentnode. removechild (node) }< BR >}