http://jsfiddle.net/MwQbE/
我有下面的 jQuery,但我无法将变量传递给第二个函数
$("img").hover(function(){
var $image = $(this);
var $imageNowWidth = $image.width();
},function() {
// get variable value for $image and $imageNowWidth
});
在 jsFiddle 上测试时它不起作用,我该怎么做才能将变量传递给第二个函数?
最佳答案
只需在 .hover
外定义这两个变量,然后您就可以在 mouseleave 函数内使用它们。见下文,
var $image, $imageNowWidth;
$("img").hover(function(){ //mouseenter
$image = $(this);
$imageNowWidth = $image.width();
},function() { //mouseleave
//$image and $imageNowWidth is accessible HERE
});
只是想澄清一下,this
将在 mouseleave
函数中可用,因此您可以在 mouseenter
中执行相同或更多的操作
关于javascript - 将变量从悬停传递到悬停,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10658015/