“这个”是什么意思?代表ajax函数内部
我一直在尝试在 ajax 函数中使用它来引用事件目标。但似乎不是我的想法。
例如:
$('#test').live('click',function(){
$.ajax({
type:'post',
url:
data:...,
success:function(mes){
$(this).append('mes');
}
});
});
所以这里 $(this) 并不引用 ('#test') 选择器。它指的是什么? 感谢您的任何解释。
I have been trying to use this inside an ajax function to refer the event target . but it seems that is not the way I think.
for example:
$('#test').live('click',function(){
$.ajax({
type:'post',
url:
data:...,
success:function(mes){
$(this).append('mes');
}
});
});
so here $(this) does not refer to the ('#test') selector. what does it refer to??
thanks for any explanation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在成功回调中,
this
引用由 jQuery 创建的全局对象,其中包含有关 AJAX 请求的信息。如果您想获取原始 DOM 元素,您可以在闭包中捕获它:或者如果您不喜欢闭包,您可以将其作为请求的键/值对传递:
这在
的情况下可能很有用success
回调不是匿名函数。Inside the success callback
this
refers to a global object created by jQuery containing information about the AJAX rerquest. If you want to get the original DOM element you could capture it in a closure:or if you don't like closures you could pass it as a key/value pair of the request:
This could be useful in scenarios where the
success
callback is not an anonymous function.默认情况下,
this
将是 AJAX 调用中使用的扩展设置对象。来自 文档:您可以使用
context
设置来指定this
将在回调中引用的对象:By default,
this
will be the extended settings object used in the AJAX call. From the documentation:You can use the
context
setting to specify the objectthis
will refer to in callbacks:这是上下文。来自 JQuery 站点:
所有回调中的
this
引用是设置中传递给 $.ajax 的上下文选项中的对象;如果未指定上下文,则这是对 Ajax 设置本身的引用。This is context. From JQuery site:
The
this
reference within all callbacks is the object in the context option passed to $.ajax in the settings; if context is not specified, this is a reference to the Ajax settings themselves.在该示例中,
$(this)
将表示$.ajax()
对象。In that example,
$(this)
would represent the$.ajax()
object.