admin管理员组文章数量:1426597
I need a little help with my code there are 366 images to change called 001.jpg, 002.jpg ... 366.jpg. It gets the date and put the picture for it. The code works but I can't get it to output in my img tag.
<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>
I need a little help with my code there are 366 images to change called 001.jpg, 002.jpg ... 366.jpg. It gets the date and put the picture for it. The code works but I can't get it to output in my img tag.
<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>
Share
Improve this question
asked Jun 21, 2011 at 21:40
Jonny SooterJonny Sooter
2,4171 gold badge24 silver badges40 bronze badges
1
- 2 wrap the code in the function img() you use in the onload tag? – C.O. Commented Jun 21, 2011 at 21:41
3 Answers
Reset to default 5Take the double-quotes off:
document.getElementById('bla').src = bgdImage;
Like I said in the initial ment, put the code in the function ìmg()
you call in the <body onload="javascript:img();">
. I also removed the quotes from your output statement to the src. document.getElementById('bla').src = bgdImage;
bgdImage
is your variable and putting it in quotes "bgdImage"
is just a String with the text bgdImage inside.
<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>
One problem in your code looks like you are setting the src of the image to the String "bgdImage" rather than the variable. Try replacing the line with this:
document.getElementById('bla').src = bgdImage;
UPDATE
It also looks like you are trying to run the code before the img
tag is set up. Try moving the script to after the img
tag, or setting a timeout to execute it after the page has pletely rendered.
本文标签: javascriptImage changing every day of the year (365 days)Stack Overflow
版权声明:本文标题:javascript - Image changing every day of the year (365 days) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745483418a2660276.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论