如何删除父元素而不删除其内容?

时间:2017-12-12 10:21:35

标签: javascript jquery

有没有办法在没有孩子的情况下移除父母?我需要删除3个父母,即“DELETE-A DELETE-B DELETE-C”类。我读过的大多数文章,只删除1个元素

或者我可以删除班级DELETE-A中的所有内容而不删除班级KASKUS-C的内容吗?因此,类DELETE-D和类DELETE-H类也将被删除

<div class="KASKUS-A">
    <div class="KASKUS-B">
        <div class="DELETE-A">
            <div class="DELETE-B">
                <div class="DELETE-C">
                    <div class="KASKUS-C">
                        <div><img src="http://www.how-to-draw-funny-cartoons.com/image-files/cartoon-owl-3f.gif" class="visible horizontal"></div>
                    </div>
                </div>
                <div class="DELETE-C">
                    <div class="KASKUS-C">
                        <div><img src="http://www.drawingcoach.com/image-files/cartoon_cow_st6.gif" class="visible horizontal"></div>
                    </div>
                </div>
            </div>
        </div>
        <div class="DELETE-D">
            <div class="DELETE-F">prev</div>
            <div class="DELETE-G">next</div>
        </div>
        <div class="DELETE-H">
            <div class="DELETE-I"><span></span></div>
            <div class="DELETE-J"><span></span></div>
        </div>
    </div>
</div>

我想成为这个

<div class="KASKUS-A">
    <div class="KASKUS-B">
        <div class="KASKUS-C">
            <div><img src="http://www.how-to-draw-funny-cartoons.com/image-files/cartoon-owl-3f.gif" class="visible horizontal"></div>
        </div>
        <div class="KASKUS-C">
            <div><img src="http://www.drawingcoach.com/image-files/cartoon_cow_st6.gif" class="visible horizontal"></div>
        </div>
    </div>
</div>

我用这个

$(document).ready(function() {
      $('.DELETE-D').remove();
      $('.DELETE-H').remove();

      $('.DELETE-A').remove();
      $('.DELETE-B').remove();
      $('.DELETE-C').not("KASKUS-C").remove();

      // and

      $('.DELETE-C:not("KASKUS-C")').remove();

      // and

      var DELETEC = $('.DELETE-C').contents();
      $(".DELETE-C").replaceWith(DELETEC);

      // and

      $('.DELETE-A').not("KASKUS-C").remove();

      // etc 

});

但仍无法正常工作https://jsfiddle.net/wwg35jeu/

1 个答案:

答案 0 :(得分:0)

如果您的元素有子节点,则可以使用JQuery unwrap()函数;如果没有子元素,则可以使用remove()

&#13;
&#13;
$('[class*="DELETE-"]').each(function(){
  if($(this).children().first().length > 0) $(this).children().first().unwrap();
  else $(this).remove();
});

console.log($('body').children().eq(1)[0]);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="KASKUS-A">
    <div class="KASKUS-B">
        <div class="DELETE-A">
            <div class="DELETE-B">
                <div class="DELETE-C">
                    <div class="KASKUS-C">
                        <div><img src="http://www.how-to-draw-funny-cartoons.com/image-files/cartoon-owl-3f.gif" class="visible horizontal"></div>
                    </div>
                </div>
                <div class="DELETE-C">
                    <div class="KASKUS-C">
                        <div><img src="http://www.drawingcoach.com/image-files/cartoon_cow_st6.gif" class="visible horizontal"></div>
                    </div>
                </div>
            </div>
        </div>
        <div class="DELETE-D">
            <div class="DELETE-F">prev</div>
            <div class="DELETE-G">next</div>
        </div>
        <div class="DELETE-H">
            <div class="DELETE-I"><span></span></div>
            <div class="DELETE-J"><span></span></div>
        </div>
    </div>
</div>
&#13;
&#13;
&#13;