原型 - getOffsetParent() 方法



此方法返回元素最接近的定位祖先。如果没有找到,则返回 body 元素。

语法

element.getOffsetParent();

返回值

返回一个 HTML 元素。

示例

<html>
   <head>
      <title>Prototype examples</title>
      <script type = "text/javascript" src = "/javascript/prototype.js"></script>
      
      <script>
         function showResult() {
            var node = $('father').getOffsetParent();
            alert("Offset Parent is : " + node.nodeName + ':' + node.innerHTML );
         }
      </script>
   </head>

   <body>
      <p>Click the button to see the result.</p>
      
      <div id = "grandfather">This is grand father
         <div id = "father"> This is father.
            <div id = "kid">This is kid</div>
         </div>
      </div>
      <br />
      
      <input type = "button" value = "showResult" onclick = "showResult();"/>
   </body>
</html>

输出

prototype_element_object.htm
广告