jQuery:除去内部元素之外的元素 [英] jQuery : remove element except inside element

查看:73
本文介绍了jQuery:除去内部元素之外的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除了元素内部,有什么方法可以删除元素:

Is there any way to remove element except inside element:

<div class="gallery">
  <a href="images/rep.png" title="rep">
    <img src="http://example.com/uploads/rep.png" class="thumbnail" alt="rep" title="rep">
  </a>
</div>

<div class="gallery">
  <img src="http://example.com/uploads/rep.png" class="thumbnail" alt="rep" title="rep">
</div>

我写了这段代码,但是没用:

I wrote this code but not work:

$(".gallery").contents().filter(".thumbnail").remove();

推荐答案

jQuery有一个unwrap()方法,该方法删除父节点并将匹配的元素保留在适当的位置:

jQuery has an unwrap() method which removes the parent node and leaves the matched element in place:

$(".gallery").contents().filter(".thumbnail").unwrap();

// or (faster)
$(".gallery .thumbnail").unwrap();

  • http://api.jquery.com/unwrap/
    • http://api.jquery.com/unwrap/
    • 这篇关于jQuery:除去内部元素之外的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆