在回调函数中访问$(this) [英] Accessing $(this) within a callback function
本文介绍了在回调函数中访问$(this)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
由于IE阻止了hint()的运行,因此我正在努力将hint()更改为jPrompt().问题是$(this)不再正常工作,因为jPrompt()不会返回值,而是使用回调函数.
I'm working on changing prompt() to jPrompt() since IE blocks prompt() from running. The problem is $(this) no longer works correctly because jPrompt() doesn't return a value, but uses a callback function.
所以可以说我有类似的东西,可以工作:
So lets say I have something like this, which works:
$("a.foo").click(function(){
$(this).text(prompt("Type Something",""));
}
当我将其转换为它时,它会崩溃:
When I convert it to this it breaks:
$("a.foo").click(function(){
jPrompt("Type something:","","", function(r) {
$(this).text(r);
}
}
如何正确访问$(this)?
How do I access $(this) properly?
推荐答案
尝试一下:
$("a.foo").click(function(){
var that = this;
jPrompt("Type something:","","", function(r) {
$(that).text(r);
}
}
这篇关于在回调函数中访问$(this)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文