假设我想对服务器进行ajax调用并使用响应替换现有文档内容的一部分。会有什么理由选择这些方法之一吗?jQuery - replaceWith与Ajax调用之间的区别或反之亦然
选项1 - 进行ajax调用,并从错误/成功函数中执行replaceWith。例如:
$.ajax({
type : 'GET',
url : '/some/path/here',
success : function(data) {
// process data here
$('#container').replaceWith(processedData);
}
});
选项2 - 呼叫replaceWith,传递函数使所述AJAX调用。例如:
$("#container").replaceWith(function(){
var responseData;
$.ajax({
type : 'GET',
url : '/some/path/here',
success : function(data) {
// process data here
responseData = processedData; //
}
});
return responseData;
});
ProcessedData从未给出任何价值。 – 2012-03-16 13:24:51