Javascript 我的脚本没有生成随机图像。有人能告诉我原因是什么吗? var mediaArray=新阵列( “images/Lab13/toshiba.jpg”, “images/Lab13/dell.jpg”, “images/Lab13/acer.jpg” ); var max=mediaArray.length; var指数=0; 函数showMedia() { randomIndex=Math.floor((Math.random()*max)); document.getElementById(“mediaimage”).src=mediaArray[randomIndex]; setTimeout(“showMedia()”,1500); }
当我加载页面时,我认为该函数工作不正常。它加载了第一幅图像“toshiba.jpg”,但它没有像预期的那样更改图像。没有函数Javascript 我的脚本没有生成随机图像。有人能告诉我原因是什么吗? var mediaArray=新阵列( “images/Lab13/toshiba.jpg”, “images/Lab13/dell.jpg”, “images/Lab13/acer.jpg” ); var max=mediaArray.length; var指数=0; 函数showMedia() { randomIndex=Math.floor((Math.random()*max)); document.getElementById(“mediaimage”).src=mediaArray[randomIndex]; setTimeout(“showMedia()”,1500); },javascript,random,Javascript,Random,当我加载页面时,我认为该函数工作不正常。它加载了第一幅图像“toshiba.jpg”,但它没有像预期的那样更改图像。没有函数ShowMedia()。JS区分大小写。您正在页面加载时调用ShowMedia(): <script type="text/javascript" > var mediaArray = new Array ( "images/Lab13/toshiba.jpg", "images/Lab13/dell.jpg", "images/Lab13/acer.jpg"
ShowMedia()
。JS区分大小写。您正在页面加载时调用ShowMedia()
:
<script type="text/javascript" >
var mediaArray = new Array (
"images/Lab13/toshiba.jpg",
"images/Lab13/dell.jpg",
"images/Lab13/acer.jpg"
);
var max = mediaArray.length;
var randomIndex=0;
function showMedia()
{
randomIndex = Math.floor((Math.random()*max));
document.getElementById("mediaimage").src= mediaArray[randomIndex];
setTimeout("showMedia()",1500);
}
</script>
</head>
<body onLoad="ShowMedia();">
<div><a href="Javascript:mediaClick(randomIndex);">
<img src="images/Lab13/toshiba.jpg" id="mediaimage" /></a>
</div>
</body>
它不存在,因为javascript区分大小写
所以正确的代码应该是:
<body onLoad="ShowMedia();">
这里有一大堆糟糕、过时的技术。您在什么地方找到这个脚本了吗?函数名为showMedia()
,但您正在调用showMedia()
。请使用JavaScript错误控制台进行调试。@Albert-如果您将firefox与firebug或chrome的内置检查器一起使用,您可以检查控制台以发现此类问题(在firebug控制台中:“ReferenceError:showMedia未定义”),非常感谢您的帮助!!!
<body onLoad="showMedia();">