Prototype - previousSiblings() 方法

此方法收集元素的所有先前同级元素,并将它们作为扩展元素数组返回。

如果两个元素具有相同的父元素,则它们为同级元素。例如,head 和 body 元素是同级元素(它们的父元素是 html 元素)。先前同级元素只是文档中位于元素之前的元素。

语法

element.previousSiblings();

返回值

返回 HTML 元素数组。

示例

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

   <body>
      <p>单击按钮查看结果。</p>
      
      <ul id = "fruits">
         <li id = "apples">
            <h3 id = "title">Apples</h3>
            <ul id = "list-of-apples">
               <li id = "golden-delicious">Golden Delicious</li>
               <li id = "mutsu">Mutsu</li>
               <li id = "mcintosh" class = "yummy">McIntosh</li>
               <li id = "ida-red" class = "yummy">Ida Red</li>
            </ul>
            <p id = "saying">An apple a day keeps the doctor away.</p>  
         </li>
      </ul>
      <br />
      
      <input type = "button" value = "Show Result" onclick = "showResult();"/>
   </body>
</html>

输出

prototype_element_object.html