我可以在 hover() 中传递一个变量吗?
Can I pass a variable in hover()?
正如在下面的脚本中,我不想两次声明同一个变量 var target = xxx
并且我不想让这个变量成为全局 target = xxx
bcos 我有其他函数使用这个变量名 - 目标.
As in the script below, I don't want to declare the same variable twice var target = xxx
and I don't want to make this variable a global target = xxx
bcos I have other function using this variable name - target.
$('.image-profile').hover(function () {
var target = $('.button-change-image-profile',this);
target.show();
},function () {
//var target = $('.button-change-image-profile',this);
target.hide();
});
所以我尝试像这样传递var },function (target) {
,当然是错误的,但是还有什么其他的方法可以传递这个var呢?
So I tried to pass the var like this },function (target) {
, of course it is wrong, but any other method to pass this var?
谢谢.
短版只是在这里切换:
$('.image-profile').hover(function () {
$('.button-change-image-profile',this).toggle();
});
为了让它在每个处理程序中可用(作为更通用的解决方案)在循环时在外部定义它(使用 .each()
例如),像这样:
To have it available in each handler (as a more general solution) define it outside when looping (using .each()
for example), like this:
$('.image-profile').each(function() {
var target = $('.button-change-image-profile',this);
$(this).hover(function () {
target.show();
},function () {
target.hide();
});
});