javascript 未捕获的类型错误:无法读取未定义的属性“0”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11310577/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 12:42:52  来源:igfitidea点击:

Uncaught TypeError: Cannot read property '0' of undefined

javascriptjqueryundefined

提问by MarcusJohnson

I've been banging my head against various inanimate objects for a while. I'm using jQuery to create a promotional banner rotation thing. Every 2 seconds (i'll change it to more in the future, just for testing it's short), the image and caption change.

一段时间以来,我一直在用头撞各种无生命的物体。我正在使用 jQuery 创建一个宣传横幅旋转的东西。每 2 秒(我将在未来将其更改为更多,只是为了测试它的短),图像和标题会发生变化。

This works fine, it rotates between them and when it gets to the last one, it goes back to the beginning. But when I click on one, and it goes back to the beginning (or I click on the first one explicitly), when it next tried to rotate it throws this:

这很好用,它在它们之间旋转,当它到达最后一个时,它又回到了开头。但是当我点击一个,它回到开头(或者我明确点击第一个),当它下一次尝试旋转时,它会抛出这个:

Uncaught TypeError: Cannot read property '0' of undefined /ftwc/:178
changeImage
rotate

On my two functions. I can't understand why!

关于我的两个功能。我不明白为什么!

Here is the script:

这是脚本:

changeImage = function(num) {        
  $(".currentpromoimage").before('<div class="promoimage newpromoimage" style="background: url(\''+images[num][0]+'\') no-repeat; z-index: 1;"><div class="caption" style="color: #'+images[num][2]+'">'+images[num][1]+'</div></div>');

  $(".currentpromoimage").fadeOut(700,function(){
      $(".newpromoimage").addClass("currentpromoimage");
      $(".newpromoimage").removeClass("newpromoimage");
      $(this).remove();
  });

  $(".newpromoimage").fadeIn(700);

  $("#promoselect").children().children().css('background', '#303030');

  $("#ps_"+num).css('background', '#000000');

  currentImage = num;

};

rotate = function() {

    var imageLength = images.length-1;

    if (currentImage == imageLength) {
        changeImage("0");
    } else {
        var nextImage = currentImage+1;
        /*alert("nextImage:"+nextImage);*/  
        changeImage(nextImage);
    }

};

var selectors = "";

var preloads = '';

for(var i=0;i<images.length;i++) {
    preloads += '<img src="'+images[i][0]+'" alt="preloaded" class="preload">';

    selectors += "<li id=\"ps_"+i+"\"></li>";
 }

 $("body").prepend(preloads);

 $("#grass").after('<div id="promo"><div id="promoselect"><ul></ul></div><div class="promoimage currentpromoimage" style="background: url(\''+images[0][0]+'\') no-repeat; z-index: 1;"><div class="caption" style="color: #'+images[0][2]+'">'+images[0][1]+'</div></div></div>');

    var currentImage = 0;

    $("#promoselect").children().append(selectors);

    $("#ps_0").css('background', '#000000');

    $("#promoselect")
        .children()
        .children()
        .click(function() {
            var selector = $(this).attr("id").split("_");

            clearInterval(autoimage);

            changeImage(selector[1]);

            autoimage = setInterval(rotate,2000);   
    });

    autoimage = setInterval(rotate,2000);

This is all called inside the jQuery document ready function, and images is an array encoded from PHP using json_encode. I know it's messy at the moment but I just can't figure out why these functions are appearing undefined..

这一切都在 jQuery 文档就绪函数内部调用,图像是使用 json_encode 从 PHP 编码的数组。我知道目前它很混乱,但我无法弄清楚为什么这些函数显示为未定义..

Thanks!

谢谢!

回答by Bergi

You're using the imagevariable a lot, but don't define it anywhere (or wrong). Right in the first line, images[num]will evaluate to undefined, throwing an error when accessing images[num][0].

您经常使用该image变量,但不要在任何地方(或错误)定义它。就在第一行,images[num]将评估为undefined,在访问时抛出错误images[num][0]