jQuery 移除元素

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>
HTML

输出:

在点击按钮之前:
jQuery 移除元素

点击按钮后:
jQuery 移除元素

示例-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>
HTML

输出:

在点击按钮之前:
jQuery 移除元素

点击按钮后:
jQuery 移除元素

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册