2016-09-18 88 views

回答

5

使用not排除锚标记

$("#tagId").children().not('a').remove();

+1

很简单很好的答案 – pejman

+0

很乐意帮忙。如果这解决了您的问题,请将其标记为答案。 –

2

使用选择排除a

$("#b").click(function() { 
 
    $("#tagID").children(":not(a)").remove(); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id="tagID"> 
 
    <span>This SPAN should be removed</span><br> 
 
    <a href="#">This A should be kept</a><br> 
 
    <div>This DIV should be removed</div> 
 
</div> 
 
<button id="b">Click me</button>

0

有不需要的jQuery的解决方案:

var container = document.getElementById('container') 

Array.from(container.children).filter(function(child) { 
    return !(child instanceof HTMLAnchorElement) 
}).forEach(function(child) { 
    container.removeChild(child) 
})