Jquery-mobile-dynamic-collapsible

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

jQuery Mobile-動的折りたたみ可能

説明

折りたたみは、折りたたみセットに動的に追加できます。

次の例は、jQuery Mobileでの_dynamic collapsible_の使用を示しています。

<!DOCTYPE html>
<html>
   <head>
      <title>Dynamic collapsible</title>
      <meta name = "viewport" content = "width = device-width, initial-scale = 1">
      <link rel = "stylesheet" href = "https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">
      <script src = "https://code.jquery.com/jquery-1.11.3.min.js"></script>
      <script src = "https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
   </head>

   <body>
      <button type = "button" data-icon = "plus" data-iconpos = "right" data-mini = "true"
         data-inline = "true" id = "add_collapsible">Add collapsible</button>
      <button type = "button" data-icon = "carat-d" data-iconpos = "right" data-mini = "true"
         data-inline = "true" id = "expand_last">Expand last collapsible</button>
      <button type = "button" data-icon = "carat-u" data-iconpos = "right" data-mini = "true"
         data-inline = "true" id = "collapse_last">Collapse last</button>

      <div data-role = "collapsibleset" data-content-theme = "a" data-iconpos = "right" id = "set">
         <div data-role = "collapsible" id = "set1" data-collapsed = "true">
            <h3>Heading 1</h3>
            <p>Hi!! This is collapsible content.</p>
         </div>
      </div>

      <script>
         $( document ).on( "pagecreate", function() {
            var nextId  =  1;
            $("#add_collapsible").click(function() {
               nextId++;
               var content  =  "<div data-role = 'collapsible' id = 'set" + nextId + "'>
                  <h3>Heading " + nextId + "</h3>
                  <p>Hi!! This is collapsible content.</p></div>";
               $( "#set" ).append( content ).collapsibleset( "refresh" );
            });

            $( "#expand_last" ).click(function() {
               $("#set").children(":last").collapsible( "expand" );
            });

            $( "#collapse_last" ).click(function() {
               $( "#set" ).children( ":last" ).collapsible( "collapse" );
            });
         });
      </script>
   </body>
</html>

出力

上記のコードがどのように機能するかを確認するために、次の手順を実行してみましょう-

  • 上記のHTMLコードを dynamic_collapsiblel ファイルとしてサーバーのルートフォルダーに保存します。
  • このHTMLファイルをhttp://localhost/dynamic_collapsiblelとして開くと、次の出力が表示されます。