prependto(selector) trong jQuery



Miêu tả

Phương thức prependTo(selector) trong jQuery thêm vào trước tất cả phần tử đã so khớp tới tập hợp các phần tử đã xác định khác.

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

Cú pháp

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

selector.prependTo( selector ) 

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 prependTo(selector) trong jQuery:

  • selector − Đây là mục tiêu mà nội dung sẽ được thêm vào trước.

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 prependTo(selector) 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).prependTo("#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;"></div>
      <div class="div" style="background-color:green;"></div>
      <div class="div" style="background-color:red;"></div>
		
   </body>
	
</html>

Nó sẽ cho kết quả:

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


thao_tac_dom_trong_jquery.jsp