Javascript 清除div中除少数元素外的所有内容
假设我有一个div,其中有很多这样的元素Javascript 清除div中除少数元素外的所有内容,javascript,jquery,html,css,Javascript,Jquery,Html,Css,假设我有一个div,其中有很多这样的元素 <div id="ans" style="background-color: #FFD993; color: #FFF; overflow:auto; border: 1px outset #4f6417; -moz-border-radius: 8px; -webkit-border-radius: 8px;width:100%;text-align: center;height:33%;margin-bo
<div id="ans" style="background-color: #FFD993;
color: #FFF;
overflow:auto;
border: 1px outset #4f6417;
-moz-border-radius: 8px;
-webkit-border-radius: 8px;width:100%;text-align: center;height:33%;margin-bottom: 0px;">
<input type="button" id="again" value="See Again"></input>
<input type="button" id="prac" value="Practice"></input>
<img id="theImg" src="result/' + rand + '.png" width="40%" height="60%"/>
<a href="1.html">1</a>
<a href="2.html">2</a>
<p>This is Paragraph</p>
</div>
但它会清除里面的所有内容。如何过滤它如何删除除输入之外的所有子项
$('#ans').children(':not(input[type="button"])').remove();
您可以使用:
$('#ans').replaceWith(function() { return $('input', this) });
如果您还想保留父div,那么您可以像@adeneo建议那样:
$('#ans').find(':not(input)').remove();
它正在删除给定给div的样式
$('#ans').find(':not(input)').remove();