offsetParent() trong jQuery



Miêu tả

Phương thức offsetParent() trong jQuery trả về một tập hợp jQuery với phần tử cha đã xác định vị trí của phần tử so khớp đầu tiên.

Đây là phần tử cha đầu tiên của phần tử mà có vị trí đó (quan hệ hoặc tuyệt đối). Phương thức này chỉ làm việc với các phần tử có thể nhìn thấy.

Cú pháp

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

selector.offsetParent( )

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

  • NA

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 offsetParent() 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 () {
               var offset = $(this).offsetParent();
               $("#lresult").html("left offset: <span>" + offset.offset().left + "</span>.");
               $("#tresult").html("top offset: <span>" + offset.offset().top + "</span>.");
            });
         });
      </script>
		
      <style>
         div { width:60px; height:60px; margin:5px; float:left; }
      </style>
		
   </head>
	
   <body>
	
      <p>Click on any square:</p>
      <span id="lresult"> </span>
      <span id="tresult"> </span>
		
      <div  style="background-color:blue;">
         <div  style="background-color:pink;"></div>
      </div>
  
      <div  style="background-color:#123456;">
         <div  style="background-color:#f11;"></div>
      </div>

   </body>
	
</html>

Nó sẽ cho kết quả:

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


css_selector_trong_jquery.jsp