jQuery Mobile - 頁面載入事件



說明

在使用兩種型別的事件初始化頁面時觸發 -

  • 當頁面即將初始化時,觸發 pagebeforecreate 事件。

  • 當頁面已建立時,觸發 pagecreate 事件。

說明

在使用以下型別的事件將外部頁面載入到 DOM 中時觸發 -

  • 在頁面載入之前,觸發 pagecontainerbeforeload 事件。

  • 當頁面載入並置於 DOM 中時,觸發 pagecontainerload 事件。

  • 當頁面載入失敗並顯示“正在載入頁面中...”訊息時,觸發 pagecontainerloadfailed 事件。

示例

以下示例介紹了在 jQuery Mobile 框架中使用 頁面載入事件

<!DOCTYPE html>
   <head>
      <title>Page Load Events</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>
      
      <script>
         $(document).on("pagecontainerload",function(event,data) {
            alert("The page has been loaded: " + data.url);
         });
         $(document).on("pagecontainerloadfailed",function(event,data) {
            alert("Sorry, the page has been failed to load!!!");
         });
      </script>
   </head>
   
   <body>
      <div data-role = "page" id = "page1">
         <div data-role = "header">
            <h2>Header</h2>
         </div>

         <div data-role = "main" class = "ui-content">
            <a href = "mypage.html">My Page</a>
         </div>

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

輸出

讓我們實施以下步驟,看看上面的程式碼如何工作 -

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

  • 以 https:///jqm_page_load_event.html 的形式開啟此 HTML 檔案,將顯示以下輸出。

jquery_mobile_events.htm
廣告
© . All rights reserved.