$(this) doesn't work in a function

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

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



        
5条回答
  •  温柔的废话
    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
        }
     });
    }
    

提交回复
热议问题