本篇文章主要讲述在MyEclipse中构建一个身份证的JavaWeb项目。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>王健·身份证</title>
<script type="text/javascript">
function getCode(){
var code=document.getElementById("code").value;
var year=code.substring(6,10);
alert(year);
var month=code.substring(10,12);
alert(month);
var day=code.substring(12,14);
alert(day);
var d=new Date();
var y=d.getFullYear();
var sui=eval(y-year+1);
var sexName=code.substring(16,17);
var sex=(sexName%2==0?"女":"男");
alert(sex);
var birth=year+"/"+month+"/"+day;
var date=new Date(birth);
document.write("<p>"+"我的出生日期是"+year+"年"+month+"月"+day+"日"+",今年"+sui+"岁了"+",性别是"+sex+"</p>");
document.write("<p>"+"我是在星期"+date.getDay()+"出生的</p>");
}
</script>
</head>
<body>
<input type="text" id="code"><br />
<input type="button" value="获取出生日期及年龄" onclick="getCode()">
</body>
</html>
运行结果如下图(我在这里只展示运行中的一部分截图,想要看完整运行结果可以直接复制代码去运行一下):
JavaScript Math 对象
Math 对象
Math 对象用于执行数学任务。
Math 对象并不像 Date 和 String 那样是对象的类,因此没有构造函数 Math()。
语法
var x = Math.PI; // 返回PI
var y = Math.sqrt(16); // 返回16的平方根
Math 对象教程,请参照本站的JavaScript Math 对象教程。
Math 对象属性
属性 | 描述 |
---|---|
E | 返回算术常量 e,即自然对数的底数(约等于2.718)。 |
LN2 | 返回 2 的自然对数(约等于0.693)。 |
LN10 | 返回 10 的自然对数(约等于2.302)。 |
LOG2E | 返回以 2 为底的 e 的对数(约等于 1.414)。 |
LOG10E | 返回以 10 为底的 e 的对数(约等于0.434)。 |
PI | 返回圆周率(约等于3.14159)。 |
SQRT1_2 | 返回返回 2 的平方根的倒数(约等于 0.707)。 |
SQRT2 | 返回 2 的平方根(约等于 1.414)。 |
Math 对象方法
方法 | 描述 |
---|---|
abs(x) | 返回 x 的绝对值。 |
acos(x) | 返回 x 的反余弦值。 |
asin(x) | 返回 x 的反正弦值。 |
atan(x) | 以介于 -PI/2 与 PI/2 弧度之间的数值来返回 x 的反正切值。 |
atan2(y,x) | 返回从 x 轴到点 (x,y) 的角度(介于 -PI/2 与 PI/2 弧度之间)。 |
ceil(x) | 对数进行上舍入。 |
cos(x) | 返回数的余弦。 |
exp(x) | 返回 Ex 的指数。 |
floor(x) | 对 x 进行下舍入。 |
log(x) | 返回数的自然对数(底为e)。 |
max(x,y,z,...,n) | 返回 x,y,z,...,n 中的最高值。 |
min(x,y,z,...,n) | 返回 x,y,z,...,n中的最低值。 |
pow(x,y) | 返回 x 的 y 次幂。 |
random() | 返回 0 ~ 1 之间的随机数。 |
round(x) | 把数四舍五入为最接近的整数。 |
sin(x) | 返回数的正弦。 |
sqrt(x) | 返回数的平方根。 |
tan(x) | 返回角的正切。 |
陌然Tool获取更多程序代码
陌然计算机二级题库