做完班级网站之后,觉得有必要把过程中一些可以分享的东西写上来。首先是同学们倍感温馨的生日提示,下面的代码是经过我修改后的,具体效果请看理化检验的班级网站http://ch.sysu.edu.cn/hope/sites/2008yh/。关于生日提醒的代码在网上有不少,大家可以找找看。
生日提醒的功能由JS实现。
在js里放上如下代码:
function birthday(year,month,date,person) {
this.year=year
this.month=month
this.date=date
this.person=person
}
function birthdaylist() {
}
blist=new birthdaylist()
blist[0]= new birthday(年,月,日,"姓名1")
blist[1]= new birthday(1989,5,15,"姓名2")
\*自己增加*\
var now=new Date()
today=new Date(now.getYear(),now.getMonth(),now.getDate()) // today 0:00:00
function daysFromToday(sDate) {
return Math.round((sDate.getTime()-today.getTime())/(24*60*60*1000))
}
function writeNextBirthday(list) {
var name1="",name2="",name3="",name4="",name5="",name6="",name7="",n=0
for (var i in list) {
thisDate=new Date(today.getYear(),list[i].month-1,list[i].date)
if (daysFromToday(thisDate)<0)
thisDate.setYear(today.getYear()+1)
if (daysFromToday(thisDate)==0)
{n=n+1
document.write("<font color=##000000 style='font-size:11pt;font-family: 宋体;line-height:1.5'> ",
"<img src='Images/bir_0.jpg'> 今天是"+list[i].person+"的生日!<P>")}
}
for (var i in list) {
thisDate=new Date(today.getYear(),list[i].month-1,list[i].date)
if (daysFromToday(thisDate)<0)
thisDate.setYear(today.getYear()+1)
if (daysFromToday(thisDate)==1&&n<4)
{n=n+1
document.write("<font color=##000000 style='font-size:11pt;font-family: 宋体;line-height:1.5'> ","<img src='Images/bir_1.jpg'> "+list[i].person+",将于明天生日<P>")}
}
for (var i in list) {
thisDate=new Date(today.getYear(),list[i].month-1,list[i].date)
if (daysFromToday(thisDate)<0)
thisDate.setYear(today.getYear()+1)
if (daysFromToday(thisDate)==2&&n<4)
{n=n+1
document.write("<font color=##000000 style='font-size:11pt;font-family: 宋体;line-height:1.5'> ","<img src='Images/bir_2.jpg'> "+list[i].person+",将于"+daysFromToday(thisDate)+"天后生日<P>")}
}
for (var i in list) {
thisDate=new Date(today.getYear(),list[i].month-1,list[i].date)
if (daysFromToday(thisDate)<0)
thisDate.setYear(today.getYear()+1)
if (daysFromToday(thisDate)==3&&n<4)
{n=n+1
document.write("<font color=##000000 style='font-size:11pt;font-family: 宋体;line-height:1.5'> ","<img src='Images/bir_2.jpg'> "+list[i].person+",将于"+daysFromToday(thisDate)+"天后生日<P>")}
}
for (var i in list) {
thisDate=new Date(today.getYear(),list[i].month-1,list[i].date)
if (daysFromToday(thisDate)<0)
thisDate.setYear(today.getYear()+1)
if (daysFromToday(thisDate)==4&&n<4)
{n=n+1
document.write("<font color=##000000 style='font-size:11pt;font-family: 宋体;line-height:1.5'> ","<img src='Images/bir_2.jpg'> "+list[i].person+",将于"+daysFromToday(thisDate)+"天后生日。<P>")}
}
for (var i in list) {
thisDate=new Date(today.getYear(),list[i].month-1,list[i].date)
if (daysFromToday(thisDate)<0)
thisDate.setYear(today.getYear()+1)
if (daysFromToday(thisDate)==5&&n<4)
{n=n+1
document.write("<font color=##000000 style='font-size:11pt;font-family: 宋体;line-height:1.5'> ","<img src='Images/bir_2.jpg'> "+list[i].person+",将于"+daysFromToday(thisDate)+"天后生日<P>")}
}
for (var i in list) {
thisDate=new Date(today.getYear(),list[i].month-1,list[i].date)
if (daysFromToday(thisDate)<0)
thisDate.setYear(today.getYear()+1)
if (daysFromToday(thisDate)==6&&n<4)
{n=n+1
document.write("<font color=##000000 style='font-size:11pt;font-family: 宋体;line-height:1.5'> ","<img src='Images/bir_2.jpg'> "+list[i].person+",将于"+daysFromToday(thisDate)+"天后生日<P>")}
}
for (var i in list) {
thisDate=new Date(today.getYear(),list[i].month-1,list[i].date)
if (daysFromToday(thisDate)<0)
thisDate.setYear(today.getYear()+1)
if (daysFromToday(thisDate)==7&&n<4)
{n=n+1
document.write("<font color=##000000 style='font-size:11pt;font-family: 宋体;line-height:1.5'> ","<img src='Images/bir_2.jpg'> "+list[i].person+",将于"+daysFromToday(thisDate)+"天后生日<P>")}
}
\*这里设置的是按离生日近到远的顺序,显示7天内生日的好友,最多显示4条。具体需要自己改.img那行是字前面的小图标,自己做*\
}
在body里,你需要的地方加上如下代码(当然这是table的,还有位置等问题,大家自己换代码吧):
<tr>
<td vAlign=center><SCRIPT LANGUAGE="JavaScript">
<!--
writeNextBirthday(blist)
// -->
</SCRIPT> <SCRIPT LANGUAGE="JavaScript">
<!-- hide
function goHist(a)
{
history.go(a);
}
//-->
</script></td>
</tr>