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

继续 寻找 jquery each 不能深度 遍历 的 问题 解决
代码的目的是让所有的span class 失效,包括span 嵌套中的 span , 现在的问题是不能 把span 嵌套中的 span class 失效.见代码:


<!DOCTYPE html >  
<html>  
<head>  
<meta  charset="UTF-8">  
<title>replace</title>  
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
 
 $('#r').click(function(){

 $('*').each(function(){
        if(this.tagName=='SPAN'){ 
         var t = $(this).html(); 
         var c = $(this).attr('class');  
         var a = new Array();
         a = c.split(' ');
         var classhide = '' ;
         $.each(a,function(index,callback){
           
          classhide+=a[index]+"-- ";
 
         })
 $(this).replaceWith(' <span class="'+classhide+'">  '+t+' </span> ');       
        }
 
      });

 alert(id1.outerHTML)
})
  


  });

</script>  
</head>  
<body>  
<div id='id1' style="width: 600px; height: 300px;"> 
<span   class="blue bolder">
          Hi <i>Hello</i>
     <span   class="underline red">   And </span>  
          <b>Goodbye</b>
</span>  
<span   class="oblique green"> How Are You </span>  
</div> 
<button id="r">click</button> <br />
      
</body>  
</html>
<style type="text/css">
 .blue{color: blue;  }
 .green{color: green;  }
 .red{color: red;  }
 .oblique{font-style: oblique;}
 .bolder{font-style: bolder;}
 .underline{text-decoration:underline}
 .hidden{visibility:hidden;}
 .visible{visibility:visible;}
</style>

 

------解决方案--------------------
遍历下样式替换为其他的,然后再替换回来就行了

<!DOCTYPE html >  
<html>  
<head>  
<meta  charset="UTF-8">  
<title>replace</title>  
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script type="text/javascript">