简体   繁体   English

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

[英]Attempting to automatically have an image change depending on the date

The website I am working on is for taking football bookings, but I have a section on the website that features upcoming fixtures, and to cut out somebody having to change the code after every game I wanted to implement a fixture image that automatically changes depending on the date IE so that it changes itself after the date of the match. 我正在工作的网站用于预订足球,但是我在网站上有一个版块,其中包含即将推出的装置,并希望在每场比赛后我都想更改某个代码,以免有人更改代码以实现自动更改装置图像。日期IE,以便它在比赛日期之后自行更改。

I have played around and I cannot see what I am missing... here is what I did: 我玩过游戏,看不到我想念的东西...这是我所做的:

http://jsfiddle.net/gLffJ/180/ 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;
}

Any help would be much appreciated! 任何帮助将非常感激!

Thanks 谢谢

Here you go. 干得好。

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;
}

btw, remember that getMonth returns months from 0-11 not 1-12 so you may need to change your 8 to a 7. 顺便说一句,请记住,getMonth返回的是0-11而不是1-12的月份,因此您可能需要将8更改为7。

Please check following updated image tag and JavaScript function. 请检查以下更新的图像标签和JavaScript功能。 I commented first line in function and change ID 我在功能中注释了第一行并更改了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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM