去掉html中的空白text节点

记录下第一条笔记

 1 //去除html用的空白节点
 2     removeWhiteSpace($(".content_toolbar").get(0));
 3     function removeWhiteSpace(elem){
 4         let el = elem || document,//必须是js对象
 5             cur = el.firstChild,
 6             temp,
 7             reg = /\S/;
 8         while(null != cur){
 9             temp = cur.nextSibling;
10             if( 3 === cur.nodeType && !reg.test(cur.nodeValue) ){//获取的节点是text类型且是空白文本
11                 el.removeChild(cur);
12             }else if( 1 === cur.nodeType ){//如果是元素则递归
13                 removeWhiteSpace(cur);
14             }
15             cur = temp;
16         }
17     }

猜你喜欢

转载自www.cnblogs.com/jiaqing521/p/11470261.html