我需要某种脚本来显示不同的内容,具体取决于星期几,主要是图像。这就是我试过的
<script>
function myFunction() {
var d = new Date();
var weekday=new Array(7);
weekday[0]="Sunday";
weekday[1]="Monday";
weekday[2]="img src="http://totalscript.ro/logo.png";
weekday[3]="Wednesday";
weekday[4]="Thursday";
weekday[5]="Friday";
weekday[6]="Saturday";
var x = document.getElementById("demo");
x.innerHTML=weekday[d.getDay()];
}
</script>
我想让它能够显示图像。你能帮忙吗?
答案 0 :(得分:1)
您可以执行以下操作:
var images = [
'http://my.site.com/img1.jpg',
'http://my.site.com/img2.jpg',
'http://my.site.com/img3.jpg',
'http://my.site.com/img4.jpg',
'http://my.site.com/img5.jpg',
'http://my.site.com/img6.jpg',
'http://my.site.com/img7.jpg',
];
function placeImage(id, images) {
document.getElementById(id).innerHTML = [
'<img src="',
images[(new Date()).getDay()],
'"/>'
].join('');
}
placeImage('demo', images);
一些增强功能
[...].join('')
是一种字符串缓冲区,对于旧浏览器可以提供更好的性能答案 1 :(得分:0)
不确定您是否只是在研究如何将图像分配给JQuery中的项目?如果就是这样
$("#demo").attr("src","http://imageLocation");
or if you want to use your already created variable, which is using raw javascript:
x.attr("src","http://imageLocation");