相关疑难解决方法(0)

$(this)在函数中不起作用

以下代码从文件加载html内容(我使用此线程)

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


$('#result').loadWithoutCache('page.html');

</script>
Run Code Online (Sandbox Code Playgroud)

请告诉我这是什么问题?我希望这是一个愚蠢的东西:)

编辑:正确的代码

<script>
$(document).ready(function() {

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

$('#result').loadWithoutCache('page.html');

});
</scipt>
Run Code Online (Sandbox Code Playgroud)

谢谢乔恩和大家!

ajax jquery load this

4
推荐指数
1
解决办法
675
查看次数

标签 统计

ajax ×1

jquery ×1

load ×1

this ×1