$(this) doesn't work in a function

前端 未结 5 1809
渐次进展
渐次进展 2020-11-30 14:44

The following code loads html content from a file (i used this thread)



        
5条回答
  •  孤城傲影
    2020-11-30 15:06

    The callback (success) function runs when the response arrives, and it doesn't run in the scope of the loadWithoutCache method, as that has already ended.

    You can use the context property in the ajax call to set the context of the callback functions:

    $.fn.loadWithoutCache = function (){
      $.ajax({
        url: arguments[0],
        cache: false,
        dataType: "html",
        context: this,
        success: function(data) {
          $(this).html(data);
        }
      });
    }
    

提交回复
热议问题