<!doctype html> <html> <head> <meta charset="gb2312"> <title>js写每周换一次div图片和名字</title> </head> <body> <div ><img id="zj_pic" src="" style="300px;"/><p id="zj_name"></p></div> <script> var pic = new Array(); var mingzi = new Array(); pic[0] = "https://gss1.bdstatic.com/-vo3dSag_xI4khGkpoWK1HF6hhy/baike/c0%3Dbaike150%2C5%2C5%2C150%2C50/sign=4fa76e93e7fe9925df01610255c135ba/7e3e6709c93d70cf4e49dc5bffdcd100baa12b30.jpg"; pic[1] = "https://gss3.bdstatic.com/-Po3dSag_xI4khGkpoWK1HF6hhy/baike/c0%3Dbaike150%2C5%2C5%2C150%2C50/sign=3c4002440c46f21fdd395601974d0005/cb8065380cd791237c57d8cbab345982b3b780de.jpg"; pic[2] = "https://gss1.bdstatic.com/9vo3dSag_xI4khGkpoWK1HF6hhy/baike/c0%3Dbaike180%2C5%2C5%2C180%2C60/sign=8884ca94b651f819e5280b18bbdd2188/f603918fa0ec08fae0ab5f185fee3d6d54fbdae2.jpg"; pic[3] = "https://gss1.bdstatic.com/-vo3dSag_xI4khGkpoWK1HF6hhy/baike/c0%3Dbaike150%2C5%2C5%2C150%2C50/sign=c7de1856f4d3572c72ef948eeb7a0842/8644ebf81a4c510f14f6ada56659252dd42aa55b.jpg"; mingzi[0]="1"; mingzi[1]="2"; mingzi[2]="3"; mingzi[3]="4"; var s1 = '2018-07-02';//开始时间 s1 = new Date(s1.replace(/-/g, "/"));//开始时间 s2 = new Date();//当前时间 var days = s2.getTime() - s1.getTime();//当前时间与定义的时间戳 var time = parseInt(days / (1000 * 60 * 60 * 24)); //当前时间与定义的时间戳天数 var yu = parseInt(time/7); var cxfz = yu%4; if(cxfz==0 ){ document.getElementById('zj_pic').src=pic[0]; document.getElementById('zj_name').innerHTML =mingzi[0]; }else if(cxfz==1){ document.getElementById('zj_pic').src=pic[1]; document.getElementById('zj_name').innerHTML =mingzi[1]; }else if(cxfz==2){ document.getElementById('zj_pic').src=pic[2]; document.getElementById('zj_name').innerHTML =mingzi[2]; }else if(cxfz==3){ document.getElementById('zj_pic').src=pic[3]; document.getElementById('zj_name').innerHTML =mingzi[3]; } </script> </body> </html>