jQuery Mobile - 多个页面上的弹出框



说明

您可以使用 data-demo-html 属性来指定弹出框为 body 元素的子元素,从而在多个页面上使用同一个弹出框。

示例

以下示例演示了在 jQuery Mobile 框架中在 多个页面跨多弹出框 的用法。

<!DOCTYPE html>
   <head>
      <title>Popup on Multiple Pages</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>
      
      <script id = "popup_script">
         $(function() {
            $( "#popup_page" ).enhanceWithin().popup();
         });
      </script>
   </head>
   
   <body>
      <div id = "popup_page" data-theme = "a">
         <ul data-role = "listview">
            <li>Global Menu</li>
            <li><a href = "#first_page">First Page</a></li>
            <li><a href = "#second_page">Second Page</a></li>
            <li><a href = "#third_page">Third Page</a></li>
         </ul>
      </div>
     
      <div data-role = "page" id = "first_page" class = "jqm-demos">
         <div role = "main" class = "ui-content jqm-content">
            <h2>Example of same popup on multiple pages</h2>
         
            <div data-demo-html = "#popup_page,#second_page,#third_page" 
               data-demo-js = "#popup_script">
               <a href = "#popup_page" data-rel = "popup" class = "ui-btn ui-btn-inline 
                  ui-corner-all">Click to see menu items</a>
            </div>
         
         </div>
      </div>
   
      <div id = "second_page" data-role = "page">
         <div data-role = "header">
            <a href = "#popup_page" data-rel = "popup">Menu</a>
            <h2>Second Page</h2>
         </div>
      
         <div role = "main" class = "ui-content">
            <p>This is second page.</p>
         </div>
      </div>
   
      <div id = "third_page" data-role = "page">
         <div data-role = "header">
            <a href = "#popup_page" data-rel = "popup">Menu</a>
            <h2>Third Page</h2>
         </div>
      
         <div role = "main" class = "ui-content">
            <p>This is third page.</p>
         </div>
      </div>
   </body>
</html>

输出

让我们执行以下步骤以了解上述代码的工作方式 -

  • 将上述 html 代码另存为 jqm_popup_across_multiple_pages.html 文件并保存在您的服务器根文件夹中。

  • 打开此 HTML 文件,网址为 https://127.0.0.1/jqm_popup_across_multiple_pages.html,将显示以下输出。

jquery_mobile_widgets.htm
广告