【问题标题】:jQuery - cannot get variable inside hover function (mouseleave)jQuery - 无法在悬停函数中获取变量(mouseleave)
【发布时间】:2013-07-08 10:13:41
【问题描述】:

我需要将current_src 的值传递给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)
    }
);

【问题讨论】:

    标签: javascript jquery jquery-hover mouseleave mousehover


    【解决方案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)
        }
    );
    

    【讨论】:

      猜你喜欢
      • 2014-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-29
      • 2013-12-25
      • 1970-01-01
      相关资源
      最近更新 更多