以下代码从文件加载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)
谢谢乔恩和大家!
所以这是我的代码,当用户点击跟随按钮时:
$('.follow_btn').click(function() {
$(this).html('<img src = "../assets/style_images/loading.gif">');
var userId = $(this).attr('id');
$.post('../assets/scripts/ajax_follow_parse.php', {
userId: userId
}, function(data) {
$(this).html(data);
});
});
Run Code Online (Sandbox Code Playgroud)
很高兴用第2行所示的加载gif替换它.但是当它返回数据并将其替换为第4行返回的数据时.
我怎样才能解决这个问题?