JavaScript实现星座查询功能 附详细代码
作者:少有挚友Y 时间:2024-05-05 09:15:43
一、题目
在文本框中输入一个生日值,点击按钮,可以显示此生日的对应星座。定义一个函数,该函数用来接收一个生日值(月日组成的4位字符串,比如“0210”,“1225”等),并根据该生日值提示属于的星座。
二、代码
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>星座查询</title>
</head>
<body>
<p align="center">
请输入一个生日值(如:0123):
<input type="text" id="t1">
<input type="button" value="显示星座" onclick="show()"/>
</p>
<script>
function show(){
var c1=document.getElementById("t1").value; //获取文本框中的值
//alert(c1);
var month=c1.substring(0,2);
var day=parseInt(c1.substring(2));
switch(month){
case "01":
if(day>19){alert("水瓶座")}
else alert("摩羯座");
break;
case "02":
if(day>18){alert("双鱼座")}
else alert("水瓶座");
break;
case "03":
if(day>20){alert("白羊座")}
else alert("双鱼座");
break;
case "04":
if(day>19){alert("金牛座")}
else alert("白羊座");
break;
case "05":
if(day>20){alert("双子座")}
else alert("金牛座");
break;
case "06":
if(day>21){alert("巨蟹座")}
else alert("双子座");
break;
case "07":
if(day>22){alert("狮子座")}
else alert("巨蟹座");
break;
case "08":
if(day>22){alert("处女座")}
else alert("狮子座");
break;
case "09":
if(day>22){alert("天秤座")}
else alert("处女座");
break;
case "10":
if(day>23){alert("天蝎座")}
else alert("天秤座");
break;
case "11":
if(day>20){alert("射手座")}
else alert("天蝎座");
break;
case "12":
if(day>21){alert("摩羯座")}
else alert("射手座");
break;
}
}
</script>
</body>
</html>
三、结果
四、总结
1、首先要清楚星座与日期之间的对应的关系:
2、 substring(start,end)将返回一个包含从start到最后(不包括end)的子字符串的字符串;
parseInt()函数可解析一个字符串,并返回一个整数。
来源:https://blog.csdn.net/m0_50925590/article/details/121065111
标签:js,星座,查询
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python查询缺失值的4种方法总结
2023-10-29 13:42:08
![](https://img.aspxhome.com/file/2023/1/90871_0s.png)
python实现合并两个数组的方法
2022-05-30 21:40:11
详解Mysql命令大全(推荐)
2024-01-20 08:00:35
详解Django框架中用户的登录和退出的实现
2022-08-28 19:37:19
ORACLE常见错误代码的分析与解决(一)
2010-08-02 13:20:00
基于jQuery.validate及Bootstrap的tooltip开发气泡样式的表单校验组件思路详解
2024-04-16 10:27:29
![](https://img.aspxhome.com/file/2023/6/136916_0s.png)
Go语言提升开发效率的语法糖技巧分享
2024-05-21 10:22:39
python将字符串转换成json的方法小结
2023-11-17 23:58:57
Python3中bytes类型转换为str类型
2022-05-19 16:47:31
![](https://img.aspxhome.com/file/2023/6/111936_0s.png)
如何创建第一个Pygame程序
2022-12-05 21:02:12
![](https://img.aspxhome.com/file/2023/7/79617_0s.gif)
Microsoft .Net Remoting系列教程之一:.Net Remoting基础篇
2024-05-09 09:03:35
![](https://img.aspxhome.com/file/2023/2/129812_0s.jpg)
pandas 如何将字符串映射为数字
2021-04-15 02:47:33
python+pyqt5编写md5生成器
2022-03-11 03:06:47
![](https://img.aspxhome.com/file/2023/4/131434_0s.jpg)
PHP中的MYSQL常用函数
2010-09-30 14:49:00
Python使用random和tertools模块解一些经典概率问题
2023-03-08 13:11:37
linux下通过go语言获得系统进程cpu使用情况的方法
2024-05-08 10:13:01
python区块链创建多个交易教程
2021-05-28 13:40:42
Python3.x和Python2.x的区别介绍
2022-01-05 12:36:27
MySQL主从复制的原理及配置方法(比较详细)
2024-01-28 18:21:02
JavaScript实现九宫格抽奖
2024-04-16 09:32:06
![](https://img.aspxhome.com/file/2023/6/136596_0s.jpg)