2013-07-08 44 views
0

我需要將current_src的值傳遞給mouseleave函數。jQuery - 無法獲取懸停函數內部的變量(mouseleave)

console.log返回undefined

var current_src, swap_src; 
jQuery(".browseProductImage").hover(
    function(){ 
     var current_src = jQuery(this).attr('src'); 
     var swap_src = jQuery(this).next().attr('src'); 
     jQuery(this).attr('src', swap_src); 
    }, 
    function(){ 
     jQuery(this).attr('src', current_src); 
     console.log(current_src) 
    } 
); 

回答

1

刪除第二個varcurrent_src,你要在這裏使用上作用域一個變量:

var current_src, swap_src; 
jQuery(".browseProductImage").hover(
    function(){ 
     current_src = jQuery(this).attr('src'); 
     var swap_src = jQuery(this).next().attr('src'); 
     jQuery(this).attr('src', swap_src); 
    }, 
    function(){ 
     jQuery(this).attr('src', current_src); 
     console.log(current_src) 
    } 
); 
+0

就發現它。謝謝! – RhymeGuy

+0

因此無論如何都要考慮接受答案...... –