我有一个功能:
function test(e){
$('.movie').find('.moreInfo').removeClass('moreInfoShow');
e.parent().find('.moreInfo').addClass('moreInfoShow');
$("#movieBox").isotope('reLayout');
e.parent().find('.moreInfo').css("opacity", "0");
e.parent().find('.moreInfo').animate({opacity: 1}, 500);
}
调用者:
$("#movieBox").on('mouseover', '.moviePoster', function(event){test($(this));});
问题是我希望在页面加载时调用该函数。我需要将“this”传递给该函数,但我没有运气让它工作。
这就是我期望的工作:
test($(".moviePoster").first());
(所有代码都在“$(document).ready”函数中)
最佳答案
不带 fiddle :
更改此:
$("#movieBox").on('mouseover', '.moviePoster', function(event){test($(this));});
至
$("#movieBox").on('mouseover', '.moviePoster', test);
然后测试函数中的 $(this) 应该指向触发事件的 jQuery 对象。
使用“test($(this))”在回调中调用测试函数将立即执行该函数,而不是在事件触发时执行。
更新:
完成此操作后,您当然需要在页面加载时触发鼠标悬停事件。 请参阅 Jashwant 关于如何执行此操作的评论。
更新我错了。
function(event){test($(this));}
当然不会立即触发回调函数。只有这样才会发生这种情况:
$("#movieBox").on('mouseover', '.moviePoster', test($(this)));
抱歉!
关于javascript - 将 "this"传递给 .on 函数似乎不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12332607/