【发布时间】:2013-04-11 16:34:46
【问题描述】:
简要说明: 我知道在函数中使用 $(this) 是行不通的,因为它不在正确的范围内。我还看到了其他类似的问题。我仍然无法弄清楚如何修复我的场景。
目标: 我正在尝试使用 jQuery 构建全景照片查看器。我有它的工作,但我需要多个实例。所以我只需要定位我悬停的那个。
代码:
jsFiddle:http://jsfiddle.net/kthornbloom/5J3rh/
简化代码:
var hoverInterval;
function doStuff() {
/* The next line is the one in question */
$(this).animate({
/* stuff happening */
});
}
$(function() {
$('.pan-wrap').hover(
function() {
/* stuff happening */
hoverInterval = setInterval(doStuff, 250);
},
function() {
clearInterval(hoverInterval);
});
});
【问题讨论】:
-
我知道在函数中使用 $(this) 是行不通的,因为它不在正确的范围内。 — 范围无关紧要。重要的是上下文。
-
使用 $.proxy() 函数设置 this 的上下文。
-
看看这个小提琴jsfiddle.net/5J3rh/4
标签: javascript jquery