jQuery 移除元素
在jQuery中,为了删除元素和内容,你可以使用以下两个jQuery方法中的任何一个。
方法:
- remove() – 它用于删除所选元素(及其子元素)。
- empty() – 它用于从选定的元素中删除子元素。
示例-1:使用jQuery remove()方法。
<!DOCTYPE html>
<html>
<head>
<title>jQuery remove() Method</title>
<script src=
"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
</head>
<body>
<center>
<h1 style="color:green;">
GeeksForGeeks</h1>
<h2 id="GFG">
jQuery remove() Method</h2>
<br>
<button>Click</button>
<script>
(document).ready(function() {
("button").click(function() {
$("#GFG").remove();
});
});
</script>
</center>
</body>
</html>
输出:
在点击按钮之前:
点击按钮后:
示例-2:使用jQuery empty()方法。
<!DOCTYPE html>
<html>
<head>
<title>jQuery empty() Method
</title>
<script src=
"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
</head>
<body>
<center>
<h1 style="color:green;">
GeeksForGeeks</h1>
<h2 id="GFG"> jQuery empty() Method
</h2>
<br>
<button>Click</button>
<script>
(document).ready(function() {
("button").click(function() {
$("#GFG").empty();
});
});
</script>
</center>
</body>
</html>
输出:
在点击按钮之前:
点击按钮后: