$(this)选择器问题jquery
我在这里有这个简单的 jquery 函数。单击一个按钮,我想在 ajax 之前提醒它自己的类,并在继承时再次提醒。但是最后一种情况下的选择器 "$(this)" 不起作用并且警报返回“未定义”..
为什么?
$(".button").live("click",function(){
alert($(this).attr('class')); //returns "button"
$.ajax({
type: "POST",
url: myUrl,
data: myData,
cache: false,
success: function(html)
{
alert($(this).attr('class')); //returns undefined
}
});
i have this simple jquery function here.Clicking over a button i want to alert its own class before ajax and again upon succession..but the selector "$(this)" in the last situation is not working and the alert returns "undefined"..
why?
$(".button").live("click",function(){
alert($(this).attr('class')); //returns "button"
$.ajax({
type: "POST",
url: myUrl,
data: myData,
cache: false,
success: function(html)
{
alert($(this).attr('class')); //returns undefined
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我会这样做,将
$(this)
存储在一个变量中,这样您就可以在整个函数中使用它,而不必每次都执行 jQuery 查找,并且您也不必依赖于范围为$(this)
提供正确的元素,仅包装
this
一次,这也是性能增强I would do it like this, store
$(this)
in a variable so you can use it throughout the function without having to perform a jQuery lookup every time, and you also will not have to depend on the scope to provide the correct element for$(this)
wrapping
this
only once also is a performance enhancement这将使它工作:
您不能在嵌套函数内使用
this
引用。success
函数是一个嵌套函数,它有自己的this
值。如果您需要引用该嵌套函数内的按钮,则必须声明一个局部变量(如button
)。This will make it work:
You cannot use the
this
reference inside nested functions. Thesuccess
function is a nested function and it has its ownthis
value. If you need the reference to the button inside that nested function, you have to declare a local variable (likebutton
).尝试在声明函数时添加
var self = $(this);
,然后使用self
而不是$(this)
所以你的代码看起来像这样:
Try adding
var self = $(this);
when you declare the function, and then useself
instead of$(this)
So your code looks like this:
很多人已经发布了解决方案,所以我不会发布代码。只是想提一下原因是因为由于 success 方法是回调,因此 $(this) 的上下文不再有效。所以你需要将它分配给一个变量并存储它以供你自己使用。
Lots of people have posted the solution for this so I won't post the code. Just wanted to mention the reason is because since the success method is a callback your context of $(this) isn't valid anymore. So you need to assign it to a variable and store it for your own use.
$(this)
仅在引用 DOM 中的 HTML 对象时才存在。由于您已尝试在 AJAX 调用的成功函数中使用,因此$(this)
没有引用。例如,在下面的代码中$(this)
引用 jQuery 选择器返回的项目:您将需要使用选择器在全局范围内返回该项目,然后将其传递给AJAX 调用中的成功函数:
$(this)
only exists when referencing an HTML object in the DOM. Since you've tried using in the success function of the AJAX call,$(this)
has no reference. So for example, in the following code$(this)
refers to the item to returned by the jQuery selector:You will need to use a selector to return the item in global scope, and then pass this to the success function in the AJAX call:
请查看此处的
context
部分。基本上,您的代码中似乎发生的情况是对this
的引用不再适用。这是有道理的,因为在异步处理 AJAX 回调时代码的上下文已经发生变化。在.ajax()
调用中将context
显式设置为特定对象,将对上下文的引用携带到回调函数中。Take a look at the
context
section here. Basically, what seems to be happening in your code is that the reference tothis
no longer applies. Makes sense, given that the context of the code has moved on while the AJAX callback is being handled asynchronously. Explicitly setting thecontext
to a particular object in the.ajax()
call will carry a reference to the context into the callback function.您可以将
context: this
属性添加到传递给$.ajax
调用的哈希中,这样success
句柄将是正确设置上下文,或者您也可以执行类似的操作:或者,我见过的另一种技术:
You can either add a
context: this
property to the hash that is passed to the$.ajax
call, that way thesuccess
handle will it's context set properly, or you can also do something like:or, another technique I've seen: