我环顾四周,试图为我的问题找到解决方案,但我无法弄清楚我的代码哪里出了问题。我在我的网站上将代码分成两个文件,所以我将它们分成 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/