jQuery:删除内部元素除外的元素
有没有什么办法可以删除内部元素除外:
<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> 
我写了这个代码,但不工作:
 $(".gallery").contents().filter(".thumbnail").remove(); 
	
  jQuery有一个unwrap()方法,该方法删除父节点,并将匹配的元素留在原地: 
 $(".gallery").contents().filter(".thumbnail").unwrap(); // or (faster) $(".gallery .thumbnail").unwrap(); 
 $(".thumbnail").unwrap() 
可能是一个更简单的方法,但是:
 $('.gallery').each( function() { var img = $(this).find('img'); $(this).children("a").remove(); $(this).append(img); }); 
尝试
 innerhtml = $("div.gallery .thumbnail").get(); $("div.gallery").html(innerhtml);