当前位置: 首页 > 文档资料 > jQuery 入门教程 >

offsetParent()

优质
小牛编辑
125浏览
2023-12-01

描述 (Description)

offsetParent()方法返回第一个选定元素的定位父级(例如,相对,绝对)。

语法 (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(){
            $("p").offsetParent().addClass('hilight');
         });
      </script>
      <style>
         .hilight { background:yellow; }
      </style>
   </head>
   <body>
      <scan>Top Element</scan>
      <div style = "position:relative;">
         <div>sibling<div>child</div></div>
         <p>sibling</p>
         <scan>sibling</scan>
      <div>
   </body>
</html>

例子 (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(){
            $("p").offsetParent().addClass('hilight');
         });
      </script>
      <style>
         .hilight { background:yellow; }
      </style>
   </head>
   <body>
      <scan>Top Element</scan>
      <div class = "hilight" style = "position:relative;">
         <div>sibling<div>child</div></div>
         <p>sibling</p>
         <scan>sibling</scan>
      <div>
   </body>
</html>