编写一个JAVASCRIPT 程序,弹出一个询问生日的对话框,计算出用户的星座并显示再浏览器的状态栏上
展开全部
借此自己也学习下,写了个小例子,你看看
<html>
<head>
<script>
function show(){
var a=[['12-22','01-19','魔蝎座'],['01-20','02-18','水瓶座'],['02-19','03-20','双鱼座'],['02-21','05-20','白羊座'],
['04-21','05-20','金牛座'],['05-21','06-21','双子座'],['06-22','07-22','巨蟹座'],['07-23','08-22','狮子座'],
['08-23','09-22','处女座'],['09-23','10-22','天秤座'],['10-23','11-21','天蝎座'],['11-22','12-21','射手座']];
var datePattern = /^(\d{4})-(\d{1,2})-(\d{1,2})$/;
var birthday=window.prompt('输入你的出生年月日(格式如1985-10-15)','');
try{
if(!datePattern.test(birthday)&&birthday){
alert('请输入正确的生日格式');
}
else{
var b=new Date(Date.parse(birthday.replace('-','/')));
d=datePattern.exec(birthday);
s=RegExp.$1;
for(var i=0;i<a.length;i++){
var start_date=new Date(Date.parse(s+'/'+a[i][0].replace('-','/')));
var end_date=new Date(Date.parse((i==0?s-0+1:s)+'/'+a[i][1].replace('-','/')));
if(b>=start_date&&b<=end_date){
window.status='你的星座是:'+a[i][2];
break;
}
}
if(i=='12'){
window.status='错误的出生日期,无法解析';
}
}
}catch(e){
alert("格式错误"+e);
}
}
</script>
</head>
<body>
<input type="button" onclick='show()' value="我的星座"/>
</body>
</html>
<html>
<head>
<script>
function show(){
var a=[['12-22','01-19','魔蝎座'],['01-20','02-18','水瓶座'],['02-19','03-20','双鱼座'],['02-21','05-20','白羊座'],
['04-21','05-20','金牛座'],['05-21','06-21','双子座'],['06-22','07-22','巨蟹座'],['07-23','08-22','狮子座'],
['08-23','09-22','处女座'],['09-23','10-22','天秤座'],['10-23','11-21','天蝎座'],['11-22','12-21','射手座']];
var datePattern = /^(\d{4})-(\d{1,2})-(\d{1,2})$/;
var birthday=window.prompt('输入你的出生年月日(格式如1985-10-15)','');
try{
if(!datePattern.test(birthday)&&birthday){
alert('请输入正确的生日格式');
}
else{
var b=new Date(Date.parse(birthday.replace('-','/')));
d=datePattern.exec(birthday);
s=RegExp.$1;
for(var i=0;i<a.length;i++){
var start_date=new Date(Date.parse(s+'/'+a[i][0].replace('-','/')));
var end_date=new Date(Date.parse((i==0?s-0+1:s)+'/'+a[i][1].replace('-','/')));
if(b>=start_date&&b<=end_date){
window.status='你的星座是:'+a[i][2];
break;
}
}
if(i=='12'){
window.status='错误的出生日期,无法解析';
}
}
}catch(e){
alert("格式错误"+e);
}
}
</script>
</head>
<body>
<input type="button" onclick='show()' value="我的星座"/>
</body>
</html>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询