一.String(手册)
使用js实现阶乘
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script>function jc() {
var n = document.getElementById("num").value;var span = document.getElementById("result");if(isNaN(n)) {span.innerHTML = "请输入数字";} else {if(n<0) {span.innerHTML = "负数没有阶乘";} else if(n==0) {span.innerHTML = 1;} else {var s = 1;for(var i=n;i;i--) {s *= i;}span.innerHTML = s;}}} </script>
</head>
<body><input type="text" id="num"/><input type="button" value="阶乘"onclick="jc();"/>= <span id="result"></span>
</body>
</html>
二.Number(*)
三.数组(*)
四.Math(手册)
五.Date
- 如何创建日期对象
- 如何格式化日期对象
- 如何读写时间分量
演示js内置对象
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script>var n = 3.1415926;console.log(n.toFixed(3));var a1 = ["zhangsan",25,false];console.log(a1[0]);var a2 = new Array();a2.push("lisi");a2.push(28);a2.push(true);console.log(a2[0]);var arr = [6,12,4,1,3,8];arr.reverse();console.log(arr);arr.sort();console.log(arr);arr.sort(function(a,b){
return a-b;});console.log(arr);var d1 = new Date();var d2 = new Date("2017/05/01 11:11:11");console.log(d1);console.log(d2);console.log(d1.toLocaleDateString());console.log(d1.toLocaleTimeString());var y = d1.getFullYear();var m = d1.getMonth()+1;var d = d1.getDate();var today = y + "年" + m + "月" + d + "日";console.log(today);var str = "you can you up,no can no bb.";var reg = /no/;console.log(reg.test(str));console.log(reg.exec(str));reg = /no/g;console.log(reg.exec(str));console.log(reg.exec(str));console.log(reg.exec(str));console.log(reg.exec(str));console.log(true&&false);console.log(true&&3);console.log(true&false);console.log(true&3);function sum() {
var s = 0;if(arguments.length) {for(var i=0;i<arguments.length;i++) {s += arguments[i];}}return s;}console.log(sum(1,2));console.log(sum(1,2,3,4)); </script>
</head>
<body><p>用来演示js内置对象</p>
</body>
</html>
六.RegExp(*)
1.正则对象
用户登录验证
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style>.ok{
color:green;}.error{
color:red;} </style>
<script>function checkCode() {
console.log(1);var code = document.getElementById("code").value;var span = document.getElementById("code_msg");var reg = /^\w{5,10}$/;if(reg.test(code)) {span.className = "ok";} else {span.className = "error";}}function checkPwd() {
var pwd = document.getElementById("pwd").value;var span = document.getElementById("pwd_msg");var reg = /^\w{10,15}$/;if(reg.test(pwd)) {span.className = "ok";} else {span.className = "error";}} </script>
</head>
<body><form action="http://www.tmooc.cn"><p>账号:<input type="text" id="code"onblur="checkCode();"/><span id="code_msg">5-10位字母、数字、下划线</span></p><p>密码:<input type="password" id="pwd"onblur="checkPwd();"/><span id="pwd_msg">10-15位字母、数字、下划线</span></p><p><input type="submit" value="登录"/></p></form>
</body>
</html>
2.字符串支持正则的方法(手册)
- replace()
- match()
- search()
七.Function对象(*)
- arguments *
- eval *
案例
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script>function cal() {
var input = document.getElementById("ss");var ss = input.value;try {input.value = eval(ss);} catch(ex) {input.value = "Error";}} </script>
</head>
<body><input type="text" id="ss"/><input type="button" value="="onclick="cal();"/>
</body>
</html>