次のコードをロードするHTMLコンテンツを動作しません(私はthis threadを使用)
<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>
は、私は問題が何であるかを教えてください? 私はそれが愚かな何か:)
編集願っています:正しいコード
<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>
おかげでジョン、みんな!
:
あなたはコールバック関数のコンテキストを設定する
ajax
呼び出しでcontext
プロパティを使用することができます。 –