日期:2014-05-16  浏览次数:20296 次

如何删除 产生的 内容为空的节点

<div id="test">
<p><span>  </span> xxx </p> // 要求把:   span 空标签去掉
<p> <span>xxx <i></i></span> <b></b></p>  // 要求把: i  b 空标签去掉
</div>


------解决方案--------------------

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!-- saved from url=(0064)file:///C:/Users/Administrator/Desktop/SignIn/SignIn/SignIn.html -->
<html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script language="javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<SCRIPT type="text/javascript">
$().ready(function(){
console.log($("#test"));
console.log($("#test *"));

$("#test *").each(function(index,node){
if(node.nodeType==1 && node.innerHTML.trim()==""){
              $(node).detach();
         }
});
;}); 
</SCRIPT>
</head>
<body>

<div id="test">
<p><span>  </span> xxx </p> // 要求把:   span 空标签去掉
<p> <span>xxx <i></i></span> <b></b></p>  // 要求把: i  b 空标签去掉
</div>

</body></html>



onload 和onclick区别你自己处理吧。