jQuery Mobile - 預取頁面



說明

包括屬性 data-prefetch,我們可以將頁面預先取入單頁面模板中的 DOM 中。它有助於將這些點連結到頁面。在主頁面載入後,目標頁面會在後臺載入,“pagecreate”事件被觸發。

使用 pagecontainer 部件的 load() 方法,我們可以交替預取頁面。

$( ":mobile-pagecontainer" ).pagecontainer( "load", pageUrl, { showLoadMsg: false } );

範例

以下範例演示在 jQuery Mobile 中使用預取頁面。

<!DOCTYPE html>
<html>
   <head>
      <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>
   
   <body>
      <div data-role = "page" id = "page1">
         <div data-role = "header">
            <h2>Header</h2>
         </div>

         <div data-role = "main" class = "ui-content">
            <h2>Welcome to Tutorialspoint!!! </h2>
            <a href = "#page2" data-prefetch = "true" class = "ui-btn ui-btn-inline">Next Page</a>
         </div>

         <div data-role = "footer">
            <h2>Footer</h2>
         </div>
      </div>

      <div data-role = "page" data-dialog = "true" id = "page2">
         <div data-role = "header">
            <h2>Header</h2>
         </div>

         <div data-role = "main" class = "ui-content">
            <p>Click the link to go page one.</p>
            <a href = "#page1" class = "ui-btn">Back to Previous Page</a>
         </div>

         <div data-role = "footer">
            <h2>Footer</h2>
         </div>
      </div>
   </body>
</html>

輸出

讓我們執行以下步驟,瞭解以上程式碼如何工作:

  • 將以上 html 程式碼儲存為伺服器根資料夾中的 prefetching_page.html 檔案。

  • 將此 HTML 檔案開啟為 https:///prefetching_page.html,將顯示以下輸出。

jquery_mobile_pages.htm
廣告
© . All rights reserved.