Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/432.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.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 带有数组的For循环不工作_Javascript_Jquery - Fatal编程技术网

Javascript 带有数组的For循环不工作

Javascript 带有数组的For循环不工作,javascript,jquery,Javascript,Jquery,我有一个按钮,可以向words变量添加一个单词。当数组处于特定长度时,我希望图像的src根据数组长度而改变。似乎无法让它工作 var words = []; var str = words; for (var i = 0; i < str.length; i++) { if (str[i].length === 1) { img.src = "http://placehold.it/350x150" if (str[i].length ===

我有一个按钮,可以向
words
变量添加一个单词。当数组处于特定长度时,我希望图像的
src
根据数组长度而改变。似乎无法让它工作

var words = [];

var str = words;
for (var i = 0; i < str.length; i++) {
    if (str[i].length === 1) {
        img.src = "http://placehold.it/350x150"


        if (str[i].length === 2) {
            img.src = "http://placehold.it/200x150"

        }
    }
 }
var-words=[];
var-str=单词;
对于(变量i=0;i
如果要验证位置的长度,则需要直接验证阵列:

if (words.length === 1) {
  img.src = "http://placehold.it/350x150"
} else if (words.length === 2) {
  img.src = "http://placehold.it/200x150"
}

对于这种情况,您不需要循环

您不需要循环

var words = [];
var str = [0,"http://placehold.it/350x150", "http://placehold.it/200x150", ... ];
img.src = str[words.length] || img.src; 
这就行了

正如Oriol评论的那样,“在
if(str[i].length==1)
中嵌套
if(str[i].length==2)
是无用的。”

应该是:

var words = [];

var str = words;
for (var i = 0; i < str.length; i++) {
    if (str[i].length === 1) {
        img.src = "http://placehold.it/350x150";
    }
    else if (str[i].length === 2) {
        img.src = "http://placehold.it/200x150";
    }
}
var-words=[];
var-str=单词;
对于(变量i=0;i
假设您希望在某些断点处动态更改图像,您还必须在按钮上注册回调以确定是否需要更新

var words = [];

$(button).click(function() {
  words.push("whatever");
  var img = // get img here

  if (words.length === 1) {
    img.src = "http://placehold.it/350x150";
  } else if (words.length === 2) {
    img.src = "http://placehold.it/200x150";
  } 

  // etc
});

编辑:这里有一把小提琴,你可以看到它在工作

单词
是空的,因此
str
也是空的,所以没有什么可以循环的?我有其他代码填充单词varNesting
if(str[I]。length==2)
if(str[I]。length==1)
是无用的。(1)
img.src…后缺少分号“
(2)Define”似乎无法使其正常工作。“与你得到的相比,预期的产出是多少?控制台中显示了哪些错误?阵列开始为空。然后再加上一些东西。console.log显示单词的长度为1和2,但图像未更新?数组开始为空。然后再加上一些东西。console.log显示单词的长度为1和2,但图像没有更新?@AndrewLeonardi检查我链接的小提琴。让我知道你在找什么。数组开始是空的。然后再加上一些东西。console.log显示单词长度为1和2,但图像未更新?