开发者

how to remove matched tags but leave content with JQuery

I have HTML like this:

<div>
 <div class="a">
  content1
 </div>
 content 2
 <div class="a">
  <b>content 3</b>
 </div>
</div开发者_运维知识库>

and I want to get rid of the div's of class="a" but leave their content. My initial attempt was:

$("div.a").replaceWith($(this).html());

However this is undefined. How would you do this?


try

$("div.a").each(function(){
    $(this).replaceWith($(this).html());
});


Replacing elements with their stringified HTML content will nuke any event handlers that might be in place. This won't:

$("div.a").each(function () {
    $(this).replaceWith($(this.childNodes));
});


In jQuery you could also use contents and unwrap.

$(".parent").find(".a").contents().unwrap(); 
<div class="parent">
 <div class="a">
  content1
 </div>
 content 2
 <div class="a">
  <b>content 3</b>
 </div>
</div>

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜