简体   繁体   English

未捕获的TypeError:无法设置未定义的属性“ src”

[英]Uncaught TypeError: Cannot set property 'src' of undefined

I have the following javascript code, which is doing an image rotation on a website I'm working on. 我有以下javascript代码,该代码正在我正在处理的网站上进行图像旋转。 It works correctly in Firefox, but not in Chrome or Safari. 它可以在Firefox中正常运行,但不能在Chrome或Safari中正常运行。

When I run the script console in firebug, it doesn't find an error, but when I run the script console in Chrome, it comes back with the error: Uncaught TypeError: Cannot set property 'src' of undefined 当我在Firebug中运行脚本控制台时,找不到错误,但是当我在Chrome中运行脚本控制台时,它返回错误:Uncaught TypeError:无法设置未定义的属性“ src”

the error is at line38, near the bottom, document[place].src = new_image(); 错误在第38行底部附近,document [place] .src = new_image();

I'm new to understanding javascript, although I've used various scripts for quite a while. 尽管我已经使用各种脚本一段时间了,但我还是不了解javascript。 I would like to know whats happening thats causing the script to not work, and what I could do to make it work. 我想知道是什么原因导致脚本无法正常工作,以及如何使脚本正常工作。

var interval = 5; // delay between rotating images (in seconds)  
var random_display = 0; // 0 = no, 1 = yes  
interval *= 1000;  

var image_index = 0;  
image_list = new Array();  
image_list[image_index++] = new imageItem("images/banner/banner-1.jpg");  
image_list[image_index++] = new imageItem("images/banner/banner-1.jpg");  
image_list[image_index++] = new imageItem("images/banner/banner-2.jpg");  
image_list[image_index++] = new imageItem("images/banner/banner-3.jpg");  
image_list[image_index++] = new imageItem("images/banner/banner-4.jpg");  
image_list[image_index++] = new imageItem("images/banner/banner-5.jpg");  
image_list[image_index++] = new imageItem("images/banner/banner-6.jpg");  
var number_of_image = image_list.length;  
function imageItem(image_location) {  
this.image_item = new Image();  
this.image_item.src = image_location;  
}  
function get_ImageItemLocation(imageObj) {
return(imageObj.image_item.src)
}
function generate(x, y) {  
var range = y - x + 1;  
return Math.floor(Math.random() * range) + x;  
}  
function getNextImage() {  
if (random_display) {  
image_index = generate(0, number_of_image-1);  
}  
else {  
image_index = (image_index+1) % number_of_image;  
}  
var new_image = get_ImageItemLocation(image_list[image_index]);  
return(new_image);  
}  
function rotateImage(place) {  
var new_image = getNextImage();  
document[place].src = new_image;  
var recur_call = "rotateImage('"+place+"')";  
setTimeout(recur_call, interval);  
}  

/scripts/animation.js:38 Uncaught TypeError: Cannot set property 'src' of undefined /scripts/animation.js:38未捕获的TypeError:无法设置未定义的属性“ src”

Thanks for your help. 谢谢你的帮助。

Your code can be so much simpler. 您的代码可以简单得多。 See it in action. 看到它在行动。

Use like: rotateImage("imageid"); 用法如下: rotateImage(“ imageid”);

var interval = 5; // delay between rotating images (in seconds)  
var random_display = 0; // 0 = no, 1 = yes  
interval *= 1000;

var image_list = [ 
  "images/banner/banner-1.jpg", "images/banner/banner-1.jpg", 
  "images/banner/banner-2.jpg", "images/banner/banner-3.jpg", 
  "images/banner/banner-4.jpg", "images/banner/banner-5.jpg", 
  "images/banner/banner-6.jpg"
];

var image_index     = image_list.length;
var number_of_image = image_list.length;

function generate(x, y) {
    var range = y - x + 1;
    return Math.floor(Math.random() * range) + x;
}

function getNextImage() {
    if (random_display) {
        image_index = generate(0, number_of_image - 1);
    }
    else {
        image_index = (image_index + 1) % number_of_image;
    }
    return image_list[image_index];
}

function rotateImage(place) {
    document.getElementById(place).src = getNextImage();
    setTimeout(function() {
        rotateImage(place);
    }, interval);
}

I looks like you should be able to change the document[place].src to document.getElementById(place).src then just pass in the ID of the element and it should work. 我看起来您应该应该能够将document[place].srcdocument.getElementById(place).src然后只需传入元素的ID即可。

I've not seen using the document object as an array before but it is standard to use the getElementById method which will find the DOM element with the id you give it. 我之前从未见过将document对象用作数组,但是通常使用getElementById方法,该方法将查找具有您提供的id的DOM元素。 Once you have the correct DOM object the rest of your code should work. 一旦有了正确的DOM对象,其余的代码就可以正常工作。

What exactly are you trying to set in the line document[place].src = new_image; 您到底要在第document[place].src = new_image;行中设置什么? ? It looks like you're actually trying to set a property of a HTML element (an image in this case). 看来您实际上是在尝试设置HTML元素的属性(在这种情况下为图像)。 To do that, you should be using document.getElementById("element_id") . 为此,您应该使用document.getElementById("element_id")

At the moment, this is what is happening: in JavaScript, array notation translates to dot notation, so document[place] refers to a property of document whose name is contained in the variable place . 此刻正在发生这种情况:在JavaScript中,数组表示法转换为点表示法,因此document[place]引用了document的属性,其名称包含在变量place For example, if place is "hello" , then document[place] is the same as document.hello , which is just a property of the document object - it's just a simple variable to which you can assign anything you want - but it isn't part of the DOM. 例如,如果place"hello" ,则document[place]document.hello相同,后者只是document对象的一个​​属性-它只是一个简单的变量,您可以向其分配所需的任何内容-但这不是不是DOM的一部分。

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

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