<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title>删除元素</title>
<script type="text/javascript" src="js/jquery-1.10.2.min.js"></script>
<style type="text/css">
*{ margin:0px; padding: 0px;}
div{ width: 200px; height: 100px; background: red;}
</style>
</head>
<body>
<p>this is first graph</p>
<p>this is second graph</p>
<button class="remove">点击删除段落p</button>
<p>this is third graph</p>
<div>
<p>this is first graph</p>
<p>this is second graph</p>
<button class="empty">点击删除段落p</button>
<p>this is third graph</p>
</div>
<script type="text/javascript">
$(function(){
//jquery有;两种删除元素的方法remove()和empty()
//1.1 remove()方法
//从DOM中删除所有匹配的元素,同时返回被删除的元素,因此可以把删除的元素保存在变量中付给其他元素
$(".remove").click(function(){
var $_p = $("p").remove(); //删除段落文本
//$("body").append($_p);
});
//1.2 empty()方法
// empty()方法删除匹配的元素集合中的所有子节点
$(".empty").click(function(){
$("div").empty(); //div下的所有内容都将被删除,但是div本身还在
});
});
</script>
</body>
</html>
js删除元素
最新推荐文章于 2024-01-14 21:38:44 发布