在js里,可以使用remove()方法或empty()方法中的任何一种来删除元素和内容。
方法1:使用remove()方法
remove()方法用于删除所选元素(及其子元素)。
语法:
$(selector).remove()
示例:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<h2>jQuery remove()方法</h2>
<p>测试文本,删除!</p>
<button>删除</button>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<script>
$(document).ready(function() {
$("button").click(function() {
$("p").remove();
});
});
</script>
</body>
</html>方法2:使用empty()方法
empty()方法用于从所选元素中删除子元素。
语法:
$(selector).empty()
示例:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<h2>jQuery empty()方法</h2>
<div class="demo">
<p>测试文本,删除!</p>
<p>测试文本,删除!</p>
<p>测试文本,删除!</p>
</div>
<button>删除</button>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<script>
$(document).ready(function() {
$("button").click(function() {
$("div").empty();
});
});
</script>
</body>
</html> 



网友评论文明上网理性发言 已有0人参与
发表评论: