Jquery 将变量从我的单击事件传递给函数

Jquery 将变量从我的单击事件传递给函数,jquery,image,loading,Jquery,Image,Loading,我的意思是,有没有比使用全球网络更有效的方法?我讨厌使用全局变量,但我似乎不知道如何将href属性传递给函数 $(document).ready(function(){ var src $('.thumbs li a').click(function(){ src=$(this).attr('href') loadImage(); return false; }) function loadImage(){ var img = new Image(); $

我的意思是,有没有比使用全球网络更有效的方法?我讨厌使用全局变量,但我似乎不知道如何将href属性传递给函数

$(document).ready(function(){
var src
$('.thumbs li a').click(function(){
     src=$(this).attr('href')
     loadImage();
     return false;  
})

function loadImage(){
 var img = new Image();
 $(img).load(function () {
        //$(this).css('display', 'none'); // .hide() doesn't work in Safari when the element isn't on the DOM already
        $(this).hide();
        $('.large_img_holder').removeClass('loading').append(this);
        $(this).fadeIn();
    }).error(function () {
        // notify the user that the image could not be loaded
    }).attr('src', src );
 }
});

我不确定这是否是您想要的,但如果允许
loadImage
函数将
src
作为参数,则可以避免在
ready
函数中定义
src
变量:

$(document).ready(function(){
$('.thumbs li a').click(function(){
     var src=$(this).attr('href')
     loadImage(src);
     return false;  
})

function loadImage(src){
 var img = new Image();
 $(img).load(function () {
        //$(this).css('display', 'none'); // .hide() doesn't work in Safari when the element isn't on the DOM already
        $(this).hide();
        $('.large_img_holder').removeClass('loading').append(this);
        $(this).fadeIn();
    }).error(function () {
        // notify the user that the image could not be loaded
    }).attr('src', src );
 }
});

我不确定这是否是您想要的,但如果允许
loadImage
函数将
src
作为参数,则可以避免在
ready
函数中定义
src
变量:

$(document).ready(function(){
$('.thumbs li a').click(function(){
     var src=$(this).attr('href')
     loadImage(src);
     return false;  
})

function loadImage(src){
 var img = new Image();
 $(img).load(function () {
        //$(this).css('display', 'none'); // .hide() doesn't work in Safari when the element isn't on the DOM already
        $(this).hide();
        $('.large_img_holder').removeClass('loading').append(this);
        $(this).fadeIn();
    }).error(function () {
        // notify the user that the image could not be loaded
    }).attr('src', src );
 }
});

您只需将其作为参数传递:

function loadImage(new_src){
  var img = new Image();
  $(img).load(function () {
         //$(this).css('display', 'none'); // .hide() doesn't work in Safari when the element isn't on the DOM already
         $(this).hide();
         $('.large_img_holder').removeClass('loading').append(this);
         $(this).fadeIn();
     }).error(function () {
         // notify the user that the image could not be loaded
     }).attr('src', new_src );
  });
}

$('.thumbs li a').click(function(){
     loadImage($(this).attr('href'));
     return false;  
})

您只需将其作为参数传递:

function loadImage(new_src){
  var img = new Image();
  $(img).load(function () {
         //$(this).css('display', 'none'); // .hide() doesn't work in Safari when the element isn't on the DOM already
         $(this).hide();
         $('.large_img_holder').removeClass('loading').append(this);
         $(this).fadeIn();
     }).error(function () {
         // notify the user that the image could not be loaded
     }).attr('src', new_src );
  });
}

$('.thumbs li a').click(function(){
     loadImage($(this).attr('href'));
     return false;  
})

首先,
src
变量在
$(document).ready(function(){/*…*/};
中声明,因此它不是全局变量。此外,您可以使用
loadImage
函数的参数,而不是
src
变量:

$(document).ready(function(){
    var loadImage = function(src){
        var img = new Image();
        $(img).load(function () {
            //$(this).css('display', 'none');
            //.hide() doesn't work in Safari when the element isn't on the DOM already
            $(this).hide();
            $('.large_img_holder').removeClass('loading').append(this);
            $(this).fadeIn();
        }).error(function () {
        // notify the user that the image could not be loaded
        }).attr('src', src );
    };

    $('.thumbs li a').click(function(){
         loadImage($(this).attr('href'));
         return false;  
    });
});

首先,
src
变量在
$(document).ready(function(){/*…*/};
中声明,因此它不是全局变量。此外,您可以使用
loadImage
函数的参数,而不是
src
变量:

$(document).ready(function(){
    var loadImage = function(src){
        var img = new Image();
        $(img).load(function () {
            //$(this).css('display', 'none');
            //.hide() doesn't work in Safari when the element isn't on the DOM already
            $(this).hide();
            $('.large_img_holder').removeClass('loading').append(this);
            $(this).fadeIn();
        }).error(function () {
        // notify the user that the image could not be loaded
        }).attr('src', src );
    };

    $('.thumbs li a').click(function(){
         loadImage($(this).attr('href'));
         return false;  
    });
});

我现在不觉得可笑,我发誓我试过了。谢谢你的帮助,我很感激。我现在不觉得可笑,我发誓我试过了。谢谢你的帮助,我很感激。