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
Add a ment  | 

3 Answers 3

Reset to default 5

Take 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