Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 清除div中除少数元素外的所有内容_Javascript_Jquery_Html_Css - Fatal编程技术网

Javascript 清除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,其中有很多这样的元素

<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();