Prototype-element-previoussiblings

提供:Dev Guides
移動先:案内検索

プロトタイプ-previousSiblings()メソッド

このメソッドは、要素の以前の兄弟をすべて収集し、それらを拡張要素の配列として返します。

同じ親を持つ場合、2つの要素は兄弟です。 したがって、たとえば、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>Click the button to see the result.</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>

出力