要删除除第一个元素以外的所有元素,请将该remove()方法与slice()jQuery中的方法一起使用。您可以尝试运行以下代码以使用jQuery删除除第一个元素以外的所有元素-
<!DOCTYPE html>
<html>
<head>
<script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$(".button1").click(function(){
$("span").remove();
});
$(".button2").click(function(){
$('span').slice(1).remove();
});
});
</script>
</head>
<body>
<button class="button2">Hide all, except first element</button>
<button class="button1">Hide all the elements</button>
<span>
<p>This is demo text.</p>
<p>This is demo text.</p>
</span>
<span>
<p>This is demo text.</p>
<p>This is demo text.</p>
</span>
<span>
<p>This is demo text.</p>
<p>This is demo text.</p>
</span>
</body>
</html>