【发布时间】:2012-09-16 23:30:43
【问题描述】:
我有这样的案例——
$('<first-selector>').each(function(){
$(this).click(function(){
$('<second-selector>').click(function(){
/* Here I want to use the top $(this) reference to do some business.
I don't want to use like var thisObj = $(this) and refer it inside
the click function*/
});
});
});
如何在另一个对象中使用$(this) 引用?
【问题讨论】:
-
在进入第二个回调之前为什么不想保存
$(this)? -
@ChristopherArmstrong 这就是我不想做的。我在评论中也提到过。
-
您已经确定了唯一(明智的)解决方案。系统变量 'this' 会自动被替换以反映当前范围,因此要从不同的范围内引用它,您必须手动将其缓存在不同的变量中。
-
执行此类功能的唯一方法是使用
var thisObj = $(this)。你为什么反对使用这样的解决方案? -
你为什么要使用
each——仅仅.click就够了?
标签: javascript jquery jquery-selectors this