根据日期更改图像源 [英] Changing image source based on date
问题描述
我想要做的是根据月份和日期更改着陆页图像。麻烦是当我实现这个代码,我得到一个错误,这里是代码:
What I'm trying to do is change a landing page image based on the month and day. Trouble is when I implement this code I get an error, here's the code:
function kblogo() {
var d = new Date();
var Today = d.getDate();
var Month = d.getMonth();
var logoSrc;
if (Month == 10 && (Today >= 23 && Today <= 26)) {
logoSrc = "Images/doodles/KBThanksgiving.png";
} else if (Month == 11 && (Today >= 23 && Today <= 26)) {
logoSrc = "Images/doodles/KBHolidays.png";
} else if ((Month == 11 && Today >= 30) || (Month == 0 && Today <= 2)) {
logoSrc = "Images/doodles/KBNewYear.png";
} else if (Month == 6 && (Today >= 3 && Today <= 5)) {
logoSrc = "Images/doodles/KBJuly4.png";
} else {
logoSrc = "Images/KB.png";
}
document.getElementById("kbLogo").src = logoSrc;
}
刚刚将代码更改为我将很快测试。任何想法让我知道。
Just changed the code to this I'll be testing it shortly. Any idea's let me know.
我已经通过一些不同的验证器运行它,发现不同的东西,并修复它们,但它是固定的,因为我已经得到它,它仍然崩溃IE和FF当我实现它。
I've run it through some different validators and found different things and fixed them but it's as fixed as I have gotten it and it still crashes IE and FF when I implement it.
<img id="Logo" src="Images/default.png" alt="KnowledgeBase" width="75%" onload="logo()" />
以上是请求的img标签
Above is the requested img tag
推荐答案
您的徽标功能正在循环 - 如果默认已加载,也不需要加载默认值
Your logo function is looping - also no need to load default if default is already loaded
<img id="Logo" src="Images/default.png" alt="KnowledgeBase" width="75%" onload="logo(this)" />
function logo(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 === 10 && (Today >= 23 && Today <= 26)) {
src = "Images/doodles/blah1.png";
} else if (Month === 11 && (Today >= 23 && Today <= 26)) {
src = "Images/doodles/blah2.png";
} else if ((Month === 11 && Today >= 30) || (Month === 0 && Today <= 2)) {
src = "Images/doodles/blah3.png";
} else if (Month === 6 && (Today >= 3 && Today <= 5)) {
src = "Images/doodles/blah4.png";
}
img.src=src;
}
这篇关于根据日期更改图像源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!