Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/423.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript索引变量_Javascript - Fatal编程技术网

Javascript索引变量

Javascript索引变量,javascript,Javascript,处理索引变量以通过数组值递增。 我想我的概念是对的,但我认为有些东西 在语法上,这会阻止JavaScript工作 <html> <head> <style type="text/css"> body {font-family: arial;} </style> <script type="text/javascript"> function ChangeIt() { var colors; colors = new Array("

处理索引变量以通过数组值递增。 我想我的概念是对的,但我认为有些东西 在语法上,这会阻止JavaScript工作

<html>
<head>
<style type="text/css">

 body {font-family: arial;}
 </style>
<script type="text/javascript">
function ChangeIt()
{
var colors;
colors = new Array("red", "blue", "green", "yellow", "purple");
i=0;
document.body.style.backgroundColor = colors[0];

var t = setInterval(function() {
    i=(i>=color.length) ? 0 : i+1;
    document.body.style.backgroundColor = colors[i];
}, 3000);
}
</script>
</head>
<body>
This page begins with a red background and
changes the body background to four other colors
after three seconds.<br />

The Javascript
function is set in header section and called
from the body.
</body>
<script type="text/javascript">
ChangeIt();
</script>
</body>
</html>

正文{字体系列:arial;}
函数ChangeIt()
{
变异颜色;
颜色=新阵列(“红色”、“蓝色”、“绿色”、“黄色”、“紫色”);
i=0;
document.body.style.backgroundColor=颜色[0];
var t=setInterval(函数(){
i=(i>=颜色长度)?0:i+1;
document.body.style.backgroundColor=颜色[i];
}, 3000);
}
此页面以红色背景和
将主体背景更改为其他四种颜色
三秒钟后。
Javascript 函数在标题部分中设置并调用 从身体上。 ChangeIt();
这很简单,当设置i值时,您使用的是颜色而不是颜色

 i=(i>=colors.length) ? 0 : i+1;

现在它可以正常工作了,只需选中“将color.length替换为colors.length”

如果您查看宝贵的浏览器控制台,您将看到:

未捕获引用错误:未定义颜色

这是因为您使用的是
颜色
而不是
颜色

代码:

演示:

您在中忘记了“s”

换衣服

i=(i>=color.length) ? 0 : i+1;
进入这个

i=(i>=colors.length) ? 0 : i+1;

.

主要错误可能是您引用了
color.length
,而您的意思肯定是
colors.length

但是,关于代码的许多其他方面可以改进

  • 声明数组时,将其指定给
    颜色
  • 您可以使用数组文字而不是
    new array()
  • 在一行中声明变量,使
    i
    成为
    ChangeIt()
    的本地变量,但仍可用于区间函数的范围
  • 将“increment
    i
    ”行更改为易于阅读的行(例如,将
    0
    赋值为三元语句中的“else”)
以下是变化:

function ChangeIt() {
     var colors = ["red", "blue", "green", "yellow", "purple"], 
         i = 0,  
         fn = function() {
             i = (i < colors.length) ? i + 1 : 0;
             document.body.style.backgroundColor = colors[i];
         },
         t = setInterval(fn, 3000);
     fn(); // To set the background immediately.
};
函数ChangeIt(){
变量颜色=[“红色”、“蓝色”、“绿色”、“黄色”、“紫色”],
i=0,
fn=函数(){
i=(i
那么你想问什么?看看Javascript的for循环语法:你没有得到color.length是未定义的…它应该是colors.length。你有没有尝试在浏览器的Javascript控制台中查找错误?Bharath,谢谢。其中一个答案是“你没有得到颜色。长度未定义”。是否有一个JavaScript调试器可以提醒用户注意未定义的变量?再次感谢。Allen在DallasNo,在chrome控制台中,您将看到类似于“未捕获引用错误:颜色未定义”的内容
i=(i>=colors.length) ? 0 : i+1;
function ChangeIt() {
     var colors = ["red", "blue", "green", "yellow", "purple"], 
         i = 0,  
         fn = function() {
             i = (i < colors.length) ? i + 1 : 0;
             document.body.style.backgroundColor = colors[i];
         },
         t = setInterval(fn, 3000);
     fn(); // To set the background immediately.
};