目录

offsetParent( )

描述 (Description)

offsetParent( )方法返回一个jQuery集合,其中包含第一个匹配元素的定位父元素。

这是具有位置的元素的第一个父元素(如相对或绝对)。 此方法仅适用于可见元素。

语法 (Syntax)

以下是使用此方法的简单语法 -

<i>selector</i>.offsetParent( )

参数 (Parameters)

以下是此方法使用的所有参数的说明 -

  • NA

例子 (Example)

以下是一个简单的例子,简单地显示了这种方法的用法 -

<html>
   <head>
      <title>The jQuery Example</title>
      <script type = "text/javascript" 
         src = "https://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>

这将产生以下结果 -

新页面打开
↑回到顶部↑
WIKI教程 @2018