简体   繁体   English

Javascript和jQuery数组未定义

[英]Javascript and jQuery arrays undefined

I've tried to setup an array, but when i use console.log(array_name) , it prints the number of the counter (x), instead of printing the path of the image. 我尝试设置一个数组,但是当我使用console.log(array_name) ,它会打印计数器(x)的编号,而不是打印图像的路径。 The undefined presents itself once I tried to use console.log(img[x]) to check if the content of the variable, it's the source of the image. 一旦我尝试使用console.log(img[x])来检查变量的内容(它是图像的来源console.log(img[x]) ,那么未定义的状态就会出现。 But since img doesn't work either, I don't have any clue of what is going on. 但是由于img也不起作用,所以我对发生的事情一无所知。

$(window).on('load', function(){
    var x = 0;
    var img = [];
        $(".gallery_img").each(function(img){
            var image = new Image();
            image.src = $(this).attr("src");
            x = x + 1;
            img[x] = image.src;
            console.log(img);
            console.log($(this).attr("src"));

I'm pretty new to jquery and javascript, so I'd be very grateful for some specific explanation and not just solution. 我对jquery和javascript很陌生,所以我非常感谢您提供一些具体的说明,而不仅仅是解决方案。 I hope I've been specific enough, and not a duplicate 我希望我已经足够具体,不要重复

try to rename your array variable var img = []; 尝试重命名数组变量var img = []; to something like var imgs = []; var imgs = [];

because you're using the same variable in your function here: 因为您在此处在函数中使用相同的变量:

$(".gallery_img").each(function(img)..

Added from @guest271314 's comment. 从@ guest271314的评论中添加。

The reason why it's printing the count instead of the path because the first parameter within .each(index, element) is index of element within collection of elements 之所以打印计数而不是路径,是因为.each(index, element)的第一个参数是元素集合中元素的索引

You're incrementing your array before it has a chance to add an image to index 0. 您要递增数组,然后才有机会将图像添加到索引0。

$(window).on('load', function(){
var x = 0;
var img = [];
    $(".gallery_img").each(function(img){
        var image = new Image();
        image.src = $(this).attr("src");
        x = x + 1; //<- x becomes 1
        img[x] = image.src; //<- img[0] is undefined as img[1] is where the index began.
        console.log(img);
        console.log($(this).attr("src"));

Try changing your code to this. 尝试将您的代码更改为此。

 $(window).on('load', function(){
var x = 0;
var img = [];
    $(".gallery_img").each(function(img){
        var image = new Image();
        image.src = $(this).attr("src");
        img[x++] = image.src; //this will increment x after the value x is used.
        console.log(img);
        console.log($(this).attr("src"));

Well, while you think you passing your declared array to the anonymous function in fact you defining new local variable img with this code: 好吧,尽管您认为实际上将声明的数组传递给匿名函数,但实际上您是使用以下代码定义新的本地变量img
.each(function(img){}) that only can be seen inside this new anonymous function .each(function(img){})仅在此新的匿名函数中可见
Because this function is a callback that should have input parameters, that will be passed by each() function: jQuery.each( array, callback ) 因为此函数是应具有输入参数的回调,所以将由each()函数传递: jQuery.each(array,callback)

Now what you did, you have defined your array img in scope of function: 现在,您已经在函数范围内定义了数组img
$(window).on('load', function(){..});
And then defined one more variable as input parameter that will be used inside this function scope: 然后定义另一个变量作为输入参数,该变量将在此函数范围内使用:
$(".gallery_img").each(function(img){..});
I guess you was trying to pass this variable to this function, but this is unnecessary, since you already declared it in more wider scope and this variable already available in function scope. 我猜您正在尝试将此变量传递给此函数,但这不是必需的,因为您已经在更广泛的范围内声明了该变量,并且该变量已经在函数范围内可用。
Truth about javascript variable scopes 关于javascript变量范围的真相

When you defined this variable as a callback function parameter, you acquire your new local variable img that gets index of the match as a value and your array img become unavailable inside this function. 当您将此变量定义为回调函数参数时,您将获取新的局部变量img ,该匹配项将匹配索引作为一个值获取,并且数组img在此函数内不可用。

So what you had to do actually: 因此,您实际上必须要做的是:

$(window).on('load', function(){
  var x = 0;
  var img = [];
    $(".gallery_img").each(function(ind, val){
      var image = new Image();
      image.src = $(this).attr("src");
      // Unnecessary, we already have index - ind, unless you use some filtering.
      // So you could get rid of x variable and use ind instead, like img[ind] = image.src
      x = x + 1; //<- x becomes 1
      img[x] = image.src; //<- img[0] is undefined as img[1] is where the index began.
      console.log(img);
      console.log($(this).attr("src"));

Also I advice you to get used to jsfiddle to setup your sample code, that will help you to debug your code and us to help with your actual sample. 另外,我建议您习惯jsfiddle来设置示例代码,这将帮助您调试代码,而我们将为您提供实际示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM