显示阵列中的图像不起作用?

时间:2016-02-29 11:09:59

标签: javascript

所以我有这行代码

var imagesArray = ["1.png","2.png","3.png","4.png","5.png","6.png","7.png","8.png","9.png","10.png","11.png","12.png","13.png","14.png","15.png","16.png","17.png","18.png","19.png","20.png","21.png" ];

var newArray = new Array(100);

var repeatImage = null;

for (var i = 0; i < 100; i++)
{
  if (i === 9)
  {
    repeatImage = imagesArray[Math.floor(Math.random() * imagesArray.length)];
    newArray[i] = repeatImage;
  }

  else if(i % 9 === 0)
  {
    newArray[i] = repeatImage;
  }
  else
  {
    newArray[i] = imagesArray[Math.floor(Math.random() * imagesArray.length)];
  }
}

我需要在我的document.write

中显示这些图像

document.write("<img src='images/smileys" + imagesArray [i] + "'/>" + newArray[i] ");

但它似乎不起作用,我想我的document.write是错的,但我真的不知道在哪里

2 个答案:

答案 0 :(得分:3)

"结尾处有一个额外的双引号document.write,你应该删除它:

document.write("<img src='images/smileys" + imagesArray [i] + "'/>" + newArray[i] ");
__________________________________________________________________________________^

应该是:

document.write("<img src='images/smileys" + imagesArray [i] + "'/>" + newArray[i]);

您应该在document.write循环的末尾添加for

for (var i = 0; i < 100; i++)
{
    ...

    document.write("<img src='images/smileys" + imagesArray [i] + "'/>" + newArray[i]);
}

希望这有帮助。

答案 1 :(得分:0)

试试这个

document.write('<img src="images/smileys '+imagesArray [i]+'" /> + '+newArray[i]);

在这里工作

var imagesArray = ["1.png", "2.png", "3.png", "4.png", "5.png", "6.png", "7.png", "8.png", "9.png", "10.png", "11.png", "12.png", "13.png", "14.png", "15.png", "16.png", "17.png", "18.png", "19.png", "20.png", "21.png"];

var newArray = new Array(100);

var repeatImage = null;

for (var i = 0; i < 100; i++) {
  if (i === 9) {
    repeatImage = imagesArray[Math.floor(Math.random() * imagesArray.length)];
    newArray[i] = repeatImage;
  } else if (i % 9 === 0) {
    newArray[i] = repeatImage;
  } else {
    newArray[i] = imagesArray[Math.floor(Math.random() * imagesArray.length)];
  }
  document.write('<img src="images/smileys '+imagesArray [i]+'" /> + '+newArray[i]);
}