原型 - ancestors() 方法



此方法收集元素的所有祖先元素,並將它們作為擴充套件元素的陣列返回。

請記住,body 和 html 元素也將包括在內。

語法

element.ancestors();

返回值

HTML 元素的陣列。

示例

<html>
   <head>
      <title>Prototype examples</title>
      <script type = "text/javascript" src = "/javascript/prototype.js"></script>
      
      <script>
         function showElements() {
            var arr =  $('kid').ancestors();
            arr.each(function(node) {
               alert(node.nodeName + ': ' + node.innerHTML);
            });
         }
      </script>
   </head>

   <body>
      <div id = "father">
         <p id = "kid">This is first paragraph</p>
      </div>
      <br />
      
      <input type = "button" value = "showElements" onclick = "showElements();"/>
   </body>
</html>

輸出

prototype_element_object.htm
廣告
© . All rights reserved.