Javascript JS中随机图像脚本的问题
我试图让下面的脚本在每次刷新页面时用随机图像替换默认图像。我一直得到一个boxID为空的错误。为什么boxID为空Javascript JS中随机图像脚本的问题,javascript,html,Javascript,Html,我试图让下面的脚本在每次刷新页面时用随机图像替换默认图像。我一直得到一个boxID为空的错误。为什么boxID为空 <script type="text/javascript"> function randomThumbs(){ var unalafois=1; for (unalafois=1; unalafois<9; unalafois++){ boxID=document.getElementById("'unalafois'")
<script type="text/javascript">
function randomThumbs(){
var unalafois=1;
for (unalafois=1; unalafois<9; unalafois++){
boxID=document.getElementById("'unalafois'")
var duh= Math.ceil(Math.random()*8);
boxID.src="thumbs/'+duh+'.jpg";
}}
</script>
以下是HTML代码:
<body onload="randomThumbs()">
<table>
<tr>
<td><img id="1" src="thumbs/1.jpg" /></td>
<td><img id="2" src="thumbs/2.jpg" /></td>
<td><img id="3" src="thumbs/3.jpg" /></td>
<td><img id="4" src="thumbs/4.jpg" /></td>
<td><img id="5" src="thumbs/5.jpg" /></td>
<td><img id="6" src="thumbs/6.jpg" /></td>
<td><img id="7" src="thumbs/7.jpg" /></td>
<td><img id="8" src="thumbs/8.jpg" /></td>
</tr>
</table>
</body>
谢谢 如果要将unalafois用作变量,请删除引号: boxID=document.getElementByIdunalafois
并且不要使用以数字开头的ID,如果要符合的要求,则不合法。如果要将unalafois用作变量,请删除引号: boxID=document.getElementByIdunalafois
并且不要使用以数字开头的ID,如果您想要符合HTMLElement的ID,则这是非法的,因为HTMLElement不能以数字开头。 将ID改为img-n,其中n是一个数字
<body onload="randomThumbs()">
<table>
<tr>
<td><img id="img-1" src="thumbs/1.jpg" /></td>
<td><img id="img-2" src="thumbs/2.jpg" /></td>
<td><img id="img-3" src="thumbs/3.jpg" /></td>
<td><img id="img-4" src="thumbs/4.jpg" /></td>
<td><img id="img-5" src="thumbs/5.jpg" /></td>
<td><img id="img-6" src="thumbs/6.jpg" /></td>
<td><img id="img-7" src="thumbs/7.jpg" /></td>
<td><img id="img-8" src="thumbs/8.jpg" /></td>
</tr>
</table>
然后将脚本更改为:
<script type="text/javascript">
function randomThumbs(){
var unalafois=1;
for (unalafois=1; unalafois<9; unalafois++){
//Use var before boxID, just a best practice
var boxID = document.getElementById("img-" + unalafois)
var duh= Math.ceil(Math.random()*8);
boxID.src="thumbs/" + duh + ".jpg";
}
}
</script>
HTMLElement的ID不能以数字开头。 将ID改为img-n,其中n是一个数字
<body onload="randomThumbs()">
<table>
<tr>
<td><img id="img-1" src="thumbs/1.jpg" /></td>
<td><img id="img-2" src="thumbs/2.jpg" /></td>
<td><img id="img-3" src="thumbs/3.jpg" /></td>
<td><img id="img-4" src="thumbs/4.jpg" /></td>
<td><img id="img-5" src="thumbs/5.jpg" /></td>
<td><img id="img-6" src="thumbs/6.jpg" /></td>
<td><img id="img-7" src="thumbs/7.jpg" /></td>
<td><img id="img-8" src="thumbs/8.jpg" /></td>
</tr>
</table>
然后将脚本更改为:
<script type="text/javascript">
function randomThumbs(){
var unalafois=1;
for (unalafois=1; unalafois<9; unalafois++){
//Use var before boxID, just a best practice
var boxID = document.getElementById("img-" + unalafois)
var duh= Math.ceil(Math.random()*8);
boxID.src="thumbs/" + duh + ".jpg";
}
}
</script>
我知道了!问题是第3行的1缺少引号。以下是最后的脚本:
<script type="text/javascript">
function randomThumbs(){
var onecell="1";
for (onecell=1; onecell<9; onecell++){
var picnum=Math.ceil(Math.random()*9);
document.getElementById("img" +onecell).setAttribute("src","thumbs/"+picnum+".jpg");
}}
</script>
我知道了!问题是第3行的1缺少引号。以下是最后的脚本:
<script type="text/javascript">
function randomThumbs(){
var onecell="1";
for (onecell=1; onecell<9; onecell++){
var picnum=Math.ceil(Math.random()*9);
document.getElementById("img" +onecell).setAttribute("src","thumbs/"+picnum+".jpg");
}}
</script>
这并没有完全解决问题,但帮助很大;干杯这并没有完全解决问题,但帮助很大;干杯