jQuery - ajaxComplete( callback ) 方法



描述

ajaxComplete( callback ) 方法用於附加一個函式,該函式在每次 AJAX 請求完成時執行。這是一個 Ajax 事件。

語法

以下是使用此方法的簡單語法:

$(document).ajaxComplete( )

引數

以下是此方法使用所有引數的說明:

  • callback − 要執行的函式。用於該請求的 XMLHttpRequest 和設定作為引數傳遞給此函式。

示例

假設我們在 result.html 檔案中具有以下 HTML 內容:

<h1>THIS IS RESULT...</h1>

以下是一個簡單的示例,展示了此方法的用法。

<html>
   <head>
      <title>The jQuery Example</title>
      <script type = "text/javascript" 
         src = "https://tutorialspoint.tw/jquery/jquery-3.6.0.js">
      </script>
		
      <script type = "text/javascript" language = "javascript">
         $(document).ready(function() {
			
            $("#driver").click(function(event){
               $('#stage1').load('result.html');
            });

            $(document).ajaxComplete(function(event, request, settings){
               $("#stage2").html("<h1>Request Complete.</h1>");
            });
				
         });
      </script>
   </head>

   <body>
      <p>Click on the button to load result.html file:</p>
		
      <div id = "stage1" style = "background-color:blue;">
         STAGE - 1
      </div>
		
      <div id = "stage2" style = "background-color:blue;">
         STAGE - 2
      </div>
		
      <input type = "button" id = "driver" value = "Load Data" />
   </body>
</html>

這將產生以下結果:

jquery-ajax.htm
廣告