假设我有一个像这样的悬停事件的 javascript 函数:
hoverFunc = function (HoverElement, AnimatedElement) {
HoverElement.on({
mouseenter: function () {
AnimatedElement.hide();
}
});
}
棘手的部分是我想保持 AnimatedElement 动态,并能够将其用作 HoverElement 的“this”的引用。
这是我希望 AnimatedElement 如何运行的示例:
hoverFunc = function (HoverElement, AnimatedElement) {
HoverElement.on({
mouseenter: function () {
$(this).find("img").hide(); //this being a reference to HoverElement
}
});
}
所以我希望 AnimatedElement 能够成为 HoverElement 的“this”引用。我试过这样写:
hoverFunc($("div"), $(this).find("img"));
但显然“this”不会引用第一个参数。有没有办法做到这一点?谢谢
最佳答案
使用 event.target
代替 this
并在 mouseenter
函数调用中传递 event
mouseenter: function (event) {
$(event.target).find("img").hide(); //this being a reference to HoverElement
}
关于javascript - 如何引用传递给函数的另一个参数的 "this"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32508590/