如何使用jQuery删除除第一个元素以外的所有元素?
要删除除第一个元素以外的所有元素,请将该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>热门推荐
4 甄嬛传祝福语简短
10 中秋祝福语给父亲简短
11 入学校简短祝福语
12 写字生日祝福语简短独特
13 新婚祝福语大全简短经典
14 给女儿祝福语简短精辟
15 新年送老师简短祝福语
16 赠男友祝福语大全简短
17 朋友祝福语两字简短
18 企业励志拜年祝福语简短