$("span").each(function(){ var xx=$(this).html(); $(this).replaceWith(xx); }) <span>hi1<span>hi2hi2</span>hi1</span><span>hi3</span>  问题:
 当有span嵌套时,上面运行不给力,去除不掉嵌套中的 hi2的 span标签 ? 

解决方案 »

  1.   

    哦不对
    还是把外层的 span换成其它标签 简单点
      

  2.   

    $("span:has(span)") 试试试了不行啊
      

  3.   

    可以试试 $("apan > apan") 
      

  4.   

     <span>hi1<span>hi2hi2</span>hi1</span><span>hi3</span> 
    转成你最终生成是什么
    <span>???</span> 
      

  5.   

    去掉所有 span 标签
    $('span').remove() 
      

  6.   

    只去掉  span 标签 内容保留 
      

  7.   

    定位他们的父级
    1.  $('xxx').text();
    2.  $('xxx').html().replace(/<\/?span[^>]*?>/ig,"");
      

  8.   


     <div id="spnas">
    <span>hi1<span>hi2hi2</span>hi1</span><span>hi3</span>  
     </div>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
    <script>
      alert( $('#spnas').text() ) //简单方法
      alert( $('#spnas').html().replace(/<\/?span[^>]*?>/ig,"") ) //为保留其它标签
    $('#spnas').html(  $('#spnas').text() ) );  //重新设置HTML
    </script>
      

  9.   

    注: span 里面 有时 会有 class 的  
    <span id="id1" class="cs1">hi1<span class="cs2">hi2hi2</span>hi1</span><span>hi3</span>
      

  10.   


    谢了,有效!

    这样做的话,需要id,实际应用需要获取当前鼠标划过的文本上级id,比较麻烦.
    没有span each 这样简单.
    请问jquery中有没有比较简单的 把所有 span 无论嵌套与否全部去掉呢!万分谢谢
      

  11.   

    如同网上找到的:    function ShowDirection() {
            var tbl = [];
            var direct = (document.selection && document.selection.createRange) 
                                             ? document.selection.createRange().parentElement() // IE
                                             : window.getSelection().focusNode.parentNode; //FF
                                             alert(direct.tagName)
            do
            {
                tbl.push(direct.tagName);
            }
            while((direct = direct.parentNode) && (direct !== document.documentElement));
            alert(tbl.reverse().join('---->'))            
        }
      

  12.   

     <style>
      span{ color:red }
     </style>
     <div id="spnas">
      111 <span>hi1<span>hi2hi2</span>hi1</span><span>hi3</span> 222
     </div>
     <button id="bnt1" >clear Span</botton>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
    <script>
        $('#bnt1').click(function(){
          $('*').each(function(){
            if(this.tagName=='SPAN'){
              $(this).before( $(this).text());
              $(this).remove();
            }
          });
        });
     
    </script>
      

  13.   

    还是正则吧,有问题再说.正则中 $('#spnas').html(  $('#spnas').text() ) );  //重新设置HTML这个是错的, $('#spnas').text() )  这个不用前面的正则就可以把所有的标签去掉了.谢谢
    结贴