尝试根据日期自动更改图像

时间:2014-08-13 09:49:08

标签: javascript html image jsfiddle

我正在进行的网站是为了进行足球预订,但是我在网站上有一个部分,其中包含即将到来的灯具,并且在每场比赛后我都想要改变代码,我想自动实现一个夹具图像根据日期IE进行更改,以便在比赛日期之后自行更改。

我玩过了,我看不到我错过的东西......这就是我所做的:

http://jsfiddle.net/gLffJ/180/

I expect this to be stoke.

<img id="fix1" src="http://www.ausdenclarkbookings.co.uk/image/chels.png" alt="Fixture1" width="75%" onload="fix1(this)" />

function fix1(img) {
if (img.src.indexOf('default')==-1) return; // already changed 
var d = new Date();
var Today = d.getDate();
var Month = d.getMonth();
if ((Month === 8 && Today >= 1) || (Month === 9 && Today <= 13)) {
"http://www.ausdenclarkbookings.co.uk/image/stokefix.png";
} else if ((Month === 9 && Today >= 14) || (Month === 9 && Today <= 27)) {
"http://www.ausdenclarkbookings.co.uk/image/cpalfix.png";
} else if ((Month === 9 && Today >= 28) || (Month === 10 && Today <= 18)) {
"http://www.ausdenclarkbookings.co.uk/image/newc.png";
} 
alert(src);
img.src=src;
}

任何帮助将不胜感激!

由于

2 个答案:

答案 0 :(得分:0)

你走了。

function fix1(img) {
    //if (img.src.indexOf('default')==-1) return; // already changed 
    var d = new Date();
   var Today = d.getDate();
   var Month = d.getMonth();
   var src;
   if ((Month == 8 && Today >= 1) || (Month == 9 && Today <= 13)) {
       src = "http://www.ausdenclarkbookings.co.uk/image/stokefix.png";
   } else if ((Month == 9 && Today >= 14) || (Month == 9 && Today <= 27)) {
       src = "http://www.ausdenclarkbookings.co.uk/image/cpalfix.png";
   } else if ((Month == 9 && Today >= 28) || (Month == 10 && Today <= 18)) {
       src = "http://www.ausdenclarkbookings.co.uk/image/newc.png";
   } 
   else {
       src = 'not defined';
   }
   alert(src);
   img.src=src;
}

顺便说一句,请记住,getMonth会返回0-11而不是1-12的月份,因此您可能需要将8更改为7。

答案 1 :(得分:0)

请检查以下更新的图片代码和JavaScript功能。我在功能中注释了第一行并更改了ID

<img id="fix11" src="http://www.ausdenclarkbookings.co.uk/image/chels.png" alt="Fixture1" width="75%" onload="fix1(this)" />

function fix1(img) {
 // if (img.src.indexOf('default')==-1) return; // already changed 
 var d = new Date();
 var Today = d.getDate();
 var Month = d.getMonth();
 var src ="" ;
 if ((Month === 7 && Today >= 1) || (Month === 9 && Today <= 13)) {
   src="http://www.ausdenclarkbookings.co.uk/image/stokefix.png";
 } 
 else if ((Month === 9 && Today >= 14) || (Month === 9 && Today <= 27)) {
   src="http://www.ausdenclarkbookings.co.uk/image/cpalfix.png";
 } else if ((Month === 9 && Today >= 28) || (Month === 10 && Today <= 18)) {
  src=  "http://www.ausdenclarkbookings.co.uk/image/newc.png";
 } 
 alert(src);
img.src=src;
}