remove(expr) trong jQuery



Miêu tả

Phương thức remove(expr) trong jQuery gỡ bỏ tất cả phần tử đã so khớp từ DOM. Nó không gỡ bỏ chúng từ đối tượng jQuery, cho phép bạn sử dụng các phần tử đã so khớp với các hoạt động xa hơn.

Bạn so sánh nó với phương thức empty().

Cú pháp

Sau đây là cú pháp cho remove(expr) trong jQuery:

selector.remove( expr )

Tham số

Dưới đây miêu tả chi tiết về các tham số được sử dụng trong phương thức remove(expr) trong jQuery:

  • expr − Đây là một Expression tùy ý trong jQuery để lọc tập hợp các phần tử để bị gỡ bỏ.

Quảng cáo

Ví dụ

Sau đây là ví dụ đơn giản minh họa cách sử dụng của phương thức remove(expr) trong jQuery:

<html>
   <head>
      <title>The jQuery Example</title>
      <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
		
      <script type="text/javascript" language="javascript">
         $(document).ready(function() {
            $("div").click(function () {
               $(this).remove().appendTo("#result");
            });
         });
      </script>
		
      <style>
         .div{ margin:10px;padding:12px; border:2px solid #666; width:60px;}
      </style>
		
   </head>
	
   <body>
	
      <p>Click on any square below to see the result:</p>
      <p id="result"> THIS IS TEST </p>
      <hr />
		
      <div class="div" style="background-color:blue;">ONE</div>
      <div class="div" style="background-color:green;">TWO</div>
      <div class="div" style="background-color:red;">THREE</div>
		
   </body>
	
</html>
Quảng cáo

Nó sẽ cho kết quả:

Các bài học jQuery khác tại VietJack:


thao_tac_dom_trong_jquery.jsp