删除div周围的div

时间:2012-10-19 05:35:03

标签: jquery

我的HTML

<div class="wrapper">
  <div class="wrapper2">
     <div class="real_content"></div>
  </div>
</div>

问题

我有什么方法可以删除<div class="real_content"></div>周围的2个div,这样它就不会出现在我的网站的HTML和前端了吗?感谢。

4 个答案:

答案 0 :(得分:6)

您可以尝试.replaceWith()docs):

$('.wrapper').replaceWith( $('.real_content') );

还有一个.unwrap()函数(docs),如果您更喜欢更相对的方式:

$('.real_content').unwrap().unwrap();

答案 1 :(得分:1)

如果用jQuery替换它,代码仍将出现在HTML源代码中,以隐藏它,否则你需要在服务器端执行它。但是,要用jQuery替换DIV,可以使用jQuery.replaceWith() -

var content = $('.real_content');
$('.wrapper').replaceWith(content);

答案 2 :(得分:1)

您可以在jQuery中使用unwrap

$(".real_content").unwrap().unwrap();​

http://api.jquery.com/unwrap/

答案 3 :(得分:1)

尝试unwrap()

$('.real_content').parent().unwrap();