Prototype-element-select

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

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

このメソッドは、任意の数のCSSセレクター(文字列)を受け取り、それらのいずれかに一致する要素の拡張子孫の配列を返します。

このメソッドは()に非常に似ていますが、ドキュメント全体ではなく、1つの要素のコンテキスト内で使用できます。 サポートされているCSS構文は同一であるため、詳細については()のドキュメントを参照してください。

構文

element.select(selector...);

戻り値

HTML要素の配列を返します。

<html>
   <head>
      <title>Prototype examples</title>
      <script type = "text/javascript" src = "/javascript/prototype.js"></script>

      <script>
         function showResult() {
            var arr = $('apples').select('[title = "yummy!"]');

           //returns [h3, li#golden-delicious, li#mutsu]
            arr.each(function(node) {
               alert("First : " + node.nodeName + ': ' + node.innerHTML);
            });
            arr = $('apples').select( 'p#saying', 'li[title = "yummy!"]');

           //returns [li#golden-delicious, li#mutsu,  p#saying]
            arr.each(function(node) {
               alert("Second : " + node.nodeName + ': ' + node.innerHTML);
            });
            arr = $('apples').select('[title = "disgusting!"]');

           //returns []
            arr.each(function(node) {
               alert("Third : " + node.nodeName + ': ' + node.innerHTML);
            });
         }
      </script>
   </head>

   <body">
      <p id = "test">Click the button to see the result.</p>
      <ul id = "fruits">
         <li id = "apples">
            <h3 title = "yummy!">Apples</h3>
            <ul id = "list-of-apples">
               <li id = "golden" title = "yummy!" >Golden</li>
               <li id = "mutsu" title = "yummy!">Mutsu</li>
               <li id = "mcintosh">McIntosh</li>
               <li id = "ida-red">Ida Red</li>
            </ul>
            <p id = "saying">An apple a day keeps the doctor away.</p>
         </li>
      </ul>
      <input type = "button" value = "Click" onclick = "showResult();"/>
   </body>
</html>

出力