$(this) doesn't work in a function

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

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



        
相关标签:
5条回答
  • 2020-11-30 14:55

    You scoping of this is incorrect. You need to save your this to a variable outside of the ajax success function and reference it from that variable

    <script>
    $.fn.loadWithoutCache = function (){
     var self = this;
     $.ajax({
         url: arguments[0],
         cache: false,
         dataType: "html",
        success: function(data) {
            self.html(data);        // This is not working
          //$('#result').html(data);   //THIS WORKS!!!
            alert(data);           // This alerts the contents of page.html
        }
     });
    }
    
    
    $('#result').loadWithoutCache('page.html');
    

    0 讨论(0)
  • 2020-11-30 14:55

    Within the AJAX callback, this is bound to a different object. If you want to reuse the target of your plugin, store (capture) it in a local variable and use that.

    $.fn.loadWithoutCache = function (){
     var $target = $(this);
     $.ajax({
         url: arguments[0],
         cache: false,
         dataType: "html",
         success: function(data) {
            $target.html(data); // note change
        }
     });
    }
    
    0 讨论(0)
  • 2020-11-30 14:58

    JQuery's this is contextual. http://remysharp.com/2007/04/12/jquerys-this-demystified/

    console.log($(this)) at different points to see what it refers to.

    0 讨论(0)
  • 2020-11-30 15:00

    The problem is that inside the success callback, this does not have the value you expect it to.

    However, you do have access to this (with the expected value) inside loadWithoutCache itself. So you can achieve your goal by saving $(this) into a local variable and accessing it from inside the success handler (creating a closure).

    This is what you need to do:

    $.fn.loadWithoutCache = function (){
     var $el = $(this);
     $.ajax({
         url: arguments[0],
         cache: false,
         dataType: "html",
         success: function(data) {
            $el.html(data);
            alert(data);
        }
     });
    }
    
    0 讨论(0)
  • 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);
        }
      });
    }
    
    0 讨论(0)
提交回复
热议问题