使用Ajax背景下页面的刷新一个部分 [英] Refreshing one section of a page using Ajax context
问题描述
我想,当用户单击清除按钮,目前我使用了一下code我砍掉另外一个答案,我发现在这里刷新我的网页只是其中的一部分:
$('清晰')。点击(函数(){
$阿贾克斯({
网址:,
背景:document.body的,
成功:函数(S,X){
$(本)。html的(S);
}
});
});
这将重新加载整个文档的身体,我怎么针对特定的div或类?
背景:document.body.somediv?
jQuery的API了解在上下文
选项:
这个对象将尽一切Ajax相关回调的背景下。通过 默认情况下,上下文是重新presents阿贾克斯设置对象 在调用中使用($ .ajaxSettings合并传递给设置 $阿贾克斯)。例如,指定一个DOM元素作为上下文将会 作出这样的背景下进行的请求的完成回调,像这样:
$。阿贾克斯({ 网址:test.html中, 背景:document.body的 })。完成(功能(){ $(本).addClass(完成); });
因此,上下文
选项是设置这
对象的范围中的回调函数。默认情况下它是Ajax对象,与当前的配置:
$。阿贾克斯({
网址:
})。完成(功能(){
执行console.log(本);
//日志:{URL:中输入:GET,等等...}
//
});
如果你想针对特定部分加载的响应,你可以使用jQuery找到的元素,把该元素的响应,上下文
不与您有关:
$。阿贾克斯({
网址:,
}),成功(功能(数据){
//数据是你的回应
$(。一些元素)的HTML(数据);
});
不过,您可以使用上下文
,以使配置更容易一点了解,像这样:
$。阿贾克斯({
网址:,
背景:$(一些元素。)
}),成功(功能(数据){
$(本)。html的(数据);
// $(本)是指在上下文对象,在这种情况下,$(。一些元素)
});
如果你现在希望你的Ajax响应被其他地方加载页面上,你只需要改变选择的上下文
参数。
I'm trying to refresh just one part of my page when a user clicks a 'clear' button, currently I'm using a bit of code I hacked off another answer I found on here:
$('.clear').click(function () {
$.ajax({
url: "",
context: document.body,
success: function (s, x) {
$(this).html(s);
}
});
});
This reloads the whole document body, how do I target a particular div or class?
context: document.body.somediv?
jQuery API about the context
option:
This object will be made the context of all Ajax-related callbacks. By default, the context is an object that represents the ajax settings used in the call ($.ajaxSettings merged with the settings passed to $.ajax). For example, specifying a DOM element as the context will make that the context for the complete callback of a request, like so:
$.ajax({ url: "test.html", context: document.body }).done(function() { $( this ).addClass( "done" ); });
So, the context
option is to set the scope of the this
object in the callback functions. By default it is the ajax object, with the current configuration:
$.ajax({
url: ""
}).done(function(){
console.log(this);
// logs: { url: "", type: "GET", etc... }
//
});
If you wish to target a specific section to load the response in, you can just use jQuery to find the element and put the response in that element, context
is not relevant for you:
$.ajax({
url: "",
}).success(function(data){
// data is your response
$(".some-element").html(data);
});
However, you can use context
to make the configuration a bit easier to understand, like so:
$.ajax({
url: "",
context: $(".some-element")
}).success(function(data){
$(this).html(data);
// $(this) refers to the context object, in this case $(".some-element")
});
If you now want your ajax response to be loaded somewhere else on the page, you just have to change the selector on the context
parameter.
这篇关于使用Ajax背景下页面的刷新一个部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!