jQuery remove()
JQuery的remove()方法用于删除所有选定的元素,包括所有文本。这个方法还可以删除数据和所有选定元素的事件。
语法:
$(selector).remove()
返回值:它将返回删除的所有选定元素的数据。
示例 1:
Input: $("p").remove()
Output: Output will be all the elements of the paragraph get deleted.
代码 1:
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/
libs/jquery/3.3.1/jquery.min.js">
//this is JQuery CDN directed from the JQuery website
</script>
<script>
(document).ready(function() {
("button").click(function() {
$("p").remove();
});
});
</script>
</head>
<body>
<div style="padding-left:220px;padding-top:100px;">
<p style="font-size:35px;">Welcome to GFG!!!.</p>
<button style="padding:15px;">Click ME</button>
</div>
</body>
</html>
输出:
在点击按钮之前:
点击按钮后。
在JQuery remove()方法的帮助下,我们也可以使用它的类名来查找和删除元素。
语法:
$(".class_name").remove()
返回值:它将返回页面上所有被删除的、具有该类名称的部分。
示例 2:
Input: $(".geek").remove()
Output: Here "gfg!!!" get deleted.
代码 #2:
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs
/jquery/3.3.1/jquery.min.js">
//this is JQuery CDN directed from the JQuery website
</script>
<script>
(document).ready(function() {
("button").click(function() {
$(".geeks").remove();
});
});
</script>
</head>
<body>
<div style="margin-left:180px; font-size:35px;padding-top:100px">
<p class="geeks">Welcome to GFG!!!.</p>
<p class="geeks">Hello, My class is geeks</p>
<button>Click ME</button>
</div>
</body>
</html>
输出:
在点击按钮之前:
点击按钮后。
除了按钮之外,所有的东西都被移除