2011-10-31 102 views
3

有什么办法去除元素,除了内部因素: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(); 

回答

10

jQuery有一个unwrap()方法,它删除父节点并保留匹配的元素:

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

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

或'$( “画廊.thumbnail。”)解开();' – nnnnnn

+0

@nnnnnn:是啊,只是编辑在:-) –

+0

@AndyE:感谢安迪 – Nulled

0

可能是一个更简单的方法,但是:

$('.gallery').each(function() { 

    var img = $(this).find('img'); 
    $(this).children("a").remove(); 

    $(this).append(img); 

}); 
0

尝试

innerhtml = $("div.gallery .thumbnail").get(); 
$("div.gallery").html(innerhtml);