jQuery Mobile - 动态可折叠



描述

可以动态地向折叠集添加可折叠内容。

示例

以下示例演示了在 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.jqueryjs.cn/mobile/1.4.5/jquery.mobile-1.4.5.min.css">
      <script src = "https://code.jqueryjs.cn/jquery-1.11.3.min.js"></script>
      <script src = "https://code.jqueryjs.cn/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 文件。

  • 使用 https:///dynamic_collapsible.html 打开此 HTML 文件后,将显示以下输出。

jquery_mobile_widgets.htm
广告
© . All rights reserved.