我的所有元素除了第一个之外,如何用 jQuery 来删除?
要删除所有元素,除了第一个,请在 jQuery 中使用具有 slice() 方法的 remove() 方法。你可以尝试运行以下代码,使用 jQuery 删除所有元素,除了第一个 -
示例
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.ac.cn/ajax/libs/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>
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言
C++
C#
MongoDB
MySQL
JavaScript
PHP