
1、使用remove()方法来删除元素
remove()方法:删除被选元素及其所有子元素。 该方法也会移除被选元素的数据和事件。
remove() 方法可接收一个参数,用于删除接收参数所代表的元素,其它未接收的参数则过滤不删除。例:
无参数时:
$("#div1").remove();有参数时:
$("div").remove("#content");
示例:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("p").remove();
});
});
</script>
</head>
<body>
<p>这是一个段落。</p>
<p>这是另一个段落。</p>
<button>移除所有P元素</button>
</body>
</html>效果图:

2、使用empty()方法来删除元素
empty()方法: 删除被选元素下的所有子元素。注意:被选中的元素不会被删除。
示例:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("div").empty();
});
});
</script>
</head>
<body>
<div style="height:100px;background-color:yellow">
这是一些文本。
<p>这是div块中的一个段落。</p>
</div>
<p>这是div块外部的一个段落。</p>
<button>移除div块中的内容</button>
</body>
</html>效果图:

更多web开发知识,请查阅 HTML中文网 !!
以上就是jquery怎么删除元素?的详细内容,更多请关注易知道|edz.cc其它相关文章!














