jQuery 中 Local 事件和 Global 事件有什麼區別?


Ajax 請求會生成許多你可以訂閱的不同事件。事件有兩種型別

本地事件

這些是你可以在 Ajax 請求物件中訂閱的回撥函式。

$.ajax({
   beforeSend: function(){
      // Handle the beforeSend event
   },
   complete: function(){
     // Handle the complete event
   }
   // ......
});

全域性事件

這些事件廣播到 DOM 中的所有元素,觸發可能正在監聽的任何處理程式。你可以像這樣偵聽這些事件

$("#loading").bind("ajaxSend", function(){
   $(this).show();
 }).bind("ajaxComplete", function(){
   $(this).hide();
});

可以透過傳遞全域性選項來停用特定 Ajax 請求的全域性事件,如下所示

$.ajax({
   url: "test.html",
   global: false,
   // ...
});

更新於: 2020 年 6 月 15 日

2 千次以上的瀏覽量

開啟你的 職業生涯

透過完成課程來獲得認證

開始
廣告
© . All rights reserved.