Jquery易变量问题

Jquery易变量问题,jquery,Jquery,我试图将变量传递给Jquery中的回调函数 我有: var test; $('.img').hover(function(){ test='this is test'; }, function (){ //how do I pass the test value to the callback function..?? console.log(test) }) 谢谢你的帮助。你不需要通过它,你可以在第二个回调函数中访问test。你试过运行这个代码吗?演示:(工作正常)希

我试图将变量传递给Jquery中的回调函数

我有:

var test;

$('.img').hover(function(){
   test='this is test';

}, function (){
   //how do I pass the test value to the callback function..??
   console.log(test)

})

谢谢你的帮助。

你不需要通过它,你可以在第二个回调函数中访问
test

你试过运行这个代码吗?演示:(工作正常)希望它能满足你的需要!。。。因为变量属于外部范围。当然,在
img
class中,所有元素之间共享一个变量。
var test;
$('.img').on('hover', function(){
test ='this is a test';
console.log(test);
});