jQuery Mobile - 动态可折叠

描述

Collapsible 可以动态添加到可折叠集。


示例

以下示例演示了在 jQuery Mobile 中使用动态可折叠

<!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_collapsible.html 文件。

  • 将此 HTML 文件打开为 http://localhost/dynamic_collapsible.html,将显示以下输出。

❮ jQuery Mobile - 小部件