我需要一些帮助,我的代码中有366张图像要更改为001.jpg,002.jpg ... 366.jpg。它获取日期并为其添加图片。该代码有效,但无法在img标签中输出。

<html>
<head>
<script type="text/javascript">

var firstJan = Math.floor((new Date().setFullYear(new Date().getFullYear(),0,1))/86400000);
var today = Math.ceil((new Date().getTime())/86400000);
var dayOfYear = today-firstJan;

var bgdImage;
if((dayOfYear+'').length == 1)
bgdImage = '00'+dayOfYear+'.jpg';
else if((dayOfYear+'').length == 2)
bgdImage = '0'+dayOfYear+'.jpg';
else
bgdImage = dayOfYear+'.jpg';

document.getElementById('bla').src = "bgdImage";
</script>

</head>
<body onload=img()>

<img id="bla" width="100%" height="100%" />

</body>
</html>

最佳答案

就像我在初始评论中所说的那样,将代码放在ìmg()中调用的函数<body onload="javascript:img();">中。我还删除了输出语句中对src的引用。 document.getElementById('bla').src = bgdImage; bgdImage是您的变量,将其放在引号中"bgdImage"只是其中包含文本bgdImage的字符串。

<html>
<head>
<script type="text/javascript">

function img()
{
var firstJan = Math.floor((new Date().setFullYear(new Date().getFullYear(),0,1))/86400000);
var today = Math.ceil((new Date().getTime())/86400000);
var dayOfYear = today-firstJan;

var bgdImage;
if((dayOfYear+'').length == 1)
bgdImage = '00'+dayOfYear+'.jpg';
else if((dayOfYear+'').length == 2)
bgdImage = '0'+dayOfYear+'.jpg';
else
bgdImage = dayOfYear+'.jpg';

document.getElementById('bla').src = bgdImage;
}
</script>

</head>
<body onload="javascript:img();">

<img id="bla" width="100%" height="100%" />

</body>
</html>

09-19 06:41