微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript – 一年中每天的图像更改(365天)

我需要一点帮助我的代码有366个图像需要更改,名为001.jpg,002.jpg … 366.jpg.它获取日期并为其绘制图片.代码有效,但我无法在img标签输出.

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

var firstJan = Math.floor((new Date().setFullYear(new Date().getFullYear(),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的String.

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

function img()
{
var firstJan = Math.floor((new Date().setFullYear(new Date().getFullYear(),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>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐