请任何人解释一下javascript代码,并建议我如何为一年中的每一天做if



我找到了这个代码来在相应的一天显示每个图像。请有人解释一下以下代码,以及我如何一年365天而不是一周几天都这样做?

<script type="text/javascript"><!--
var imlocation = "images/";
 function ImageArray (n) {
   this.length = n;
   for (var i =1; i <= n; i++) {
     this[i] = ' '
   }
 }
image = new ImageArray(7);
image[0] = 'sunday.gif';
image[1] = 'monday.gif';
image[2] = 'tuesday.gif';
image[3] = 'wednesday.gif';
image[4] = 'thursday.gif';
image[5] = 'friday.gif';
image[6] = 'saturday.gif';
var currentdate = new Date();
var imagenumber = currentdate.getDay();
document.write('<img src="' + imlocation + image[imagenumber] + '">');
//--></script>

您的代码生成一个数组,并将图像放入每个键中。请阅读有关所用函数的文档,如:http://www.w3schools.com/jsref/jsref_getDay.asp

一年中某一天的图像快速变化(不太整齐):

 // http://stackoverflow.com/a/8619946
 var now = new Date();
 var start = new Date(now.getFullYear(), 0, 0);
 var diff = now - start;
 var oneDay = 1000 * 60 * 60 * 24;
 var day = Math.floor(diff / oneDay);
 var imlocation = "images/";
 function ImageArray(n) {
   this.length = n;
   for (var i = 1; i <= n; i++) {
     this[i] = ' '
   }
 }
 image = new ImageArray(365);
 image[0] = 'sunday.gif';
 // ...
 // image[365]...
 document.write('<img src="' + imlocation + image[day] + '">');
 //--></script>

最新更新