javascript - html/javascript 中的图像生成器问题

标签 javascript jquery html css stack

我环顾四周,试图为我的问题找到解决方案,但我无法弄清楚我的代码哪里出了问题。我在我的网站上将代码分成两个文件,所以我将它们分成 html 和 javascript。

HTML:

<html>
<!-- 
-->
<script type="text/javascript" src="external_javascript.js"></script>

<form name="imageForm">
  <table border=3>
  <tr>
    <td>
      <input onclick="displayImage();" type=button value="Display Random Image">
    </td>
  </tr>
  <tr>
    <td>
      <img src="blank.jpg" name="canvas" />
    </td>
  </tr>
  </table>
</form>
</html>

Javascript:

<html>

<script> 
//create an array named imagesArray that contains the seven image file names
//dog.jpg, fox.jpg, mouse.jpg, alligator.jpg, fish.jpg, parrot.jpg and cat.jpg

var imagesArray = ["a.jpg", "b.jpg", "c.jpg", "d.jpg", "e.jpg", "f.jpg", "g.jpg"];


//create a function named displayImage
//it should not have any values passed into it

function displayImage(){

    //the first statement should generate a random number in the range 0 to 6 (the subscript values of the image file names in the imagesArray)
    var num = Math.floor(Math.random() * (imagesArray.length+1)); // 0...6
    //the second statement display the random image from the imagesArray array in the canvas image using the random number as the subscript value
    document.canvas.src = imagesArray[num];

}

//remember the subscript values of the array are 0 to 6 (seven elements) zero based array
//you will have to subtract 1 from the random number generated to account for the zero based array
</script>

</html>

我还向我的网站管理员上传了从 a.jpg 到 z.jpg 的图片提前致谢!

最佳答案

你的 .js -文件应如下所示:

var imagesArray = ["a.jpg", "b.jpg", "c.jpg", "d.jpg", "e.jpg", "f.jpg", "g.jpg"];

//create a function named displayImage
//it should not have any values passed into it

function displayImage(){

    //the first statement should generate a random number in the range 0 to 6 (the subscript values of the image file names in the imagesArray)
    var num = Math.floor(Math.random() * (imagesArray.length+1)); // 0...6
    //the second statement display the random image from the imagesArray array in the canvas image using the random number as the subscript value
    document.canvas.src = imagesArray[num];
}

<html><script>是 HTML 标记的标签。

关于javascript - html/javascript 中的图像生成器问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30837043/

相关文章:

javascript - 一次等待一个查询

jquery - Ractive 模板不适用于 JQuery?

php - 使用 php 将生成的输出 html/css 转换为 pdf - 2017 年

javascript - 如何将 12 小时添加到当前日期?

html - 父div需要重叠在子div上

javascript - 在 Sublime Text 中模拟警报

javascript - Ember TextField valueBinding 到 Controller 问题

javascript - 如何更改谷歌地图 v3 中海的颜色

jquery - Cufon 行高问题

C# 创建带转义的 HTML 行