JavaScript 练习上(历史,基础语法,常用对象)
1. JS的历史
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
</body>
</html>
2. Js 与 html 结合的3种方式
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="js/index.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
var num=100;
alert(num)
</script>
</head>
<body>
<button type="button" onclick="alert('点击了')">一个按钮</button>
</body>
</html>
index.js: 放在js文件夹下
var str="Hello";
alert(str);
3. 数据类型
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script type="text/javascript">
var num=100
var a=3.24
var s="abc"
var str='hehe'
var flag=true
var flag2=false
var v;
var obj=null;
alert(num);
alert(a);
alert(s);
alert(str);
alert(flag);
alert(flag2);
alert(v);
alert(obj);
alert(typeof num);
alert(typeof a);
alert(typeof s);
alert(typeof str);
alert(typeof flag);
alert(typeof flag2);
alert(typeof v);
alert(typeof obj);
</script>
</head>
<body>
</body>
</html>
4. 逻辑语句
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script type="text/javascript">
var num = 200;
if (num >= 200) {
alert("真")
} else {
alert("假的")
}
for(var i=1;i<=10;i++){
alert(i);
}
</script>
</head>
<body>
</body>
</html>
5. 打印的方法
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script type="text/javascript">
var num=100;
document.write(num);
document.write("<h1>你好JS</h1>")
document.write("<h1 style='color: red;'>你好世界</h1>")
document.write("<h1 style='color:blue;'>你好世界"+num+"</h1>")
document.write("<b style='color:blue;'>你好世界"+num+"</b>")
document.write("<br>")
document.write("<b style='color:blue;'>你好世界"+num+"</b>")
</script>
</head>
<body>
</body>
</html>
5. 输出语句到控制台查看
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script type="text/javascript">
var num=100;
var sal=3.25;
console.log(num);
console.log(num,sal);
</script>
</head>
<body>
</body>
</html>
练习:打印99乘法表
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script type="text/javascript">
for(var i=1;i<=9;i++){
for(var j=1;j<=i;j++){
document.write(j+"*"+i+"="+(j*i)+" ")
}
document.write("<br>");
}
</script>
</head>
<body>
</body>
</html>
6. 算数运算符:+ - * / %
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script type="text/javascript">
var str = "2";
str = "abc100";
var num = str * 5;
var flag = false;
var sum2 = 2 * flag;
alert(sum2)
alert(5 * "");
alert(undefined/2);
</script>
</head>
<body>
</body>
</html>
7. 一元运算符:++ – +(正) -(负)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script type="text/javascript">
var i=1;
var j=++i;
var a=+"100";
var sum=100+a;
alert(sum);
var s="100"*1+"200"*1;
alert(s)
</script>
</head>
<body>
</body>
</html>
8. 比较运算符: > < >= <= == 三个等于=== !=:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script type="text/javascript">
var v = NaN;
var b = v == NaN;
alert(b);
alert(NaN != NaN)
var str = "abc";
var str2 = "heheheh";
alert(str == str2)
var e="100";
var f=100;
var bb=e==f;
alert(bb);
var bb2=e===f;
alert(bb2);
</script>
</head>
<body>
</body>
</html>
9. 逻辑运算符 && || !
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script type="text/javascript">
var str="abc";
var num=10;
var vv=null;
vv=new Object();
if(vv){
alert("真")
}else{
alert("假的");
}
</script>
</head>
<body>
</body>
</html>
10. 函数
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script type="text/javascript">
function add(a,b){
var sum=a+b;
return sum;
}
var r=add(20,30);
console.log(r);
var show=function(a,b){
var sum=a+b;
return sum;
}
var vv=show(36,69);
alert(vv);
</script>
</head>
<body>
</body>
</html>
函数的注意事项:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script type="text/javascript">
function add(a, b) {
console.log(a);
console.log(b);
return a + b;
}
var v = add(10, 20, 30, 250);
alert(v);
var len=add.length;
alert(len);
function show(){
console.log(arguments[0]);
console.log(arguments[1]);
console.log(arguments[2]);
console.log(arguments[3]);
console.log(arguments[4]);
var sum=0;
for(var i=0;i<arguments.length;i++){
sum+=arguments[i];
}
return sum;
}
var ss=show(20,30,52,30,85);
console.log("结果",ss);
</script>
</head>
<body>
</body>
</html>
11. 自定义函数
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script type="text/javascript">
var sum = (function(a, b) {
return a + b;
}(20, 63));
alert(sum);
</script>
</head>
<body>
</body>
</html>
12. void运算符
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script type="text/javascript">
</script>
</head>
<body>
<a href="javascript:window.open('about:blank')">Click me</a>
<a href="javascript:void(window.open('about:blank'))">Click me</a>
</body>
</html>
只想让 a可以点,但是不要跳页面:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script type="text/javascript">
function show(){
alert("show 函数")
}
</script>
</head>
<body>
<a href="index.html">一个链接</a>
<a href="#">一个链接</a>
<a href="javascript:void(0)">一个链接www.163.com</a>
<a href="javascript:;;">一个链接www.163.com</a>
<a href="javascript:void(show())">一个按钮</a>
</body>
</html>
13. const和let关键词
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script type="text/javascript">
const num = 200;
{
var a = 1000;
let b = 500;
}
alert(a);
if(true){
let c = 500;
}
for(let i=1;i<=10;i++){
alert(i);
}
</script>
</head>
<body>
</body>
</html>
14. 局部变量和全局变量
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script type="text/javascript">
const num = 200;
{
var a = 1000;
let b = 500;
}
alert(a);
if(true){
let c = 500;
}
for(let i=1;i<=10;i++){
alert(i);
}
</script>
</head>
<body>
</body>
</html>
JS 提供的常用几个内置对象 ES5之前没有类这个概念,只是对象这个概念 ES6之后有了类这个概念
15. String对象
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script type="text/javascript">
var str=new String("hello");
var s=String("abc");
var ss="abc"
alert(str instanceof String);
alert(s instanceof String);
alert(typeof str);
alert(typeof s);
</script>
</head>
<body>
</body>
</html>
16. Number对象
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script type="text/javascript">
var myNum=new Number(1000);
var myNum2=Number("3.25");
alert(myNum instanceof Number);
alert(myNum2 instanceof Number);
alert(typeof myNum);
alert(typeof myNum2);
var max=Number.MAX_VALUE;
var min=Number.MIN_VALUE;
var v=new Number(3.58585858585);
var ss=v.toFixed(5);
alert(ss);
var yy="100";
var n=yy*1;
var n2=+yy;
var n3=Number(yy);
</script>
</head>
<body>
</body>
</html>
17. Math对象
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script type="text/javascript">
var n=Math.round(Math.random()*100)
alert(n)
</script>
</head>
<body>
</body>
</html>
练习:验证码
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script type="text/javascript">
for(let i=0;i<1000;i++){
var num=Math.ceil(Math.random()*100);
if(num==1||num==100){
console.log(num);
}
}
var str="0123456789abcdefghjklABCDEFGHJKLZXY";
var a=Math.floor(Math.random()*str.length);
var b=Math.floor(Math.random()*str.length);
var c=Math.floor(Math.random()*str.length);
var d=Math.floor(Math.random()*str.length);
var code=str.charAt(a).concat(str.charAt(b)).concat(str.charAt(c)).concat(str.charAt(d));
alert(code);
var strCode='';
for(let i=1;i<=6;i++){
var v=Math.ceil(Math.random()*9);
strCode+=v
}
alert(strCode);
</script>
</head>
<body>
</body>
</html>
18. Date日期对象
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script type="text/javascript">
var myDate=new Date();
var year=myDate.getFullYear();
var month=myDate.getMonth();
var day=myDate.getDay();
var d=myDate.getDate();
var hour=myDate.getHours()
var minu=myDate.getMinutes();
var sec=myDate.getSeconds();
var time=myDate.getTime();
alert(year)
alert(month);
alert(day)
alert(d)
alert(hour);
alert(minu);
alert(sec)
alert(time)
var localTime=myDate.toLocaleString();
document.write(localTime);
</script>
</head>
<body>
</body>
</html>
19. 使用第三方的工具类来处理日期----moment
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="js/moment.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
var dateStr = moment().format('YYYY-MM-DD HH:mm:ss');
alert(dateStr);
var time = new Date().getTime();
var dateStr2 = moment(time).format('YYYY-MM-DD HH:mm:ss');
document.write("<h1>" + dateStr2 + "</h1>");
var a = moment('2019-08-22 12:30:23').unix()
alert(a);
</script>
</head>
<body>
</body>
</html>
20.全局函数
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script type="text/javascript">
var b=isNaN(NaN);
alert(b);
var b1=isNaN("abc"*1);
alert(b1);
var yy="100";
var n=yy*1;
var n2=+yy;
var n3=Number(yy);
var num=parseInt("100abc");
alert(num);
var num2=parseFloat("3.25abc");
alert(num2);
</script>
</head>
<body>
</body>
</html>
21. 数组对象
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script type="text/javascript">
var arr=new Array();
arr[0]=10;
arr[1]=20;
arr[2]=30;
arr[3]=40;
arr[4]=50;
var ele=arr[arr.length-1];
alert(ele);
var ele2=arr[10];
alert(ele2);
var arr2=new Array(3);
arr2[0]=20;
arr2[1]=30;
arr2[2]=40;
arr2[3]=200;
alert(arr2[3]);
var arr3=new Array(10,false,"abc",304,3.25);
var arr4=[10,false,"abc",304,3.25];
for(let i=0;i<arr4.length;i++){
console.log(arr4[i])
}
</script>
</head>
<body>
</body>
</html>
22. 数组对象常用的方法
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script type="text/javascript">
var arr=[10,20,30,40,50,60];
var arr2=[100,200,300,400,500,600];
var newArr=arr.concat(arr2,arr2);
console.log(newArr);
var str=arr.join("-");
console.log(str);
var str2=arr.join("");
console.log(str2);
var arr3=[100,200,300,400,500,600];
var ele=arr3.pop();
ele=arr3.pop();
ele=arr3.pop();
ele=arr3.pop();
console.log(arr3);
console.log(ele);
var arr4=[];
arr[0]=10;
arr4.push(20);
arr4.push(100,200,300);
console.log(arr4);
console.log(arr4.reverse());
var arr5=[100,200,300,400,500,600];
var ele2=arr5.shift();
ele2=arr5.shift();
ele2=arr5.shift();
console.log(ele2);
console.log(arr5);
</script>
</head>
<body>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script type="text/javascript">
var arr = [100, 200, 300, 400, 500, 600];
arr.splice(2,1);
console.log(arr);
var arr2 = [100, 200, 300, 400, 500, 600];
arr2.splice(2,2);
console.log(arr2);
var arr3 = [100, 200, 300, 400, 500, 600];
arr3.splice(2,1,7000);
console.log(arr3)
var arr4 = [100, 200, 300, 400, 500, 600];
arr4.splice(2,1,7000,8000,9000);
console.log(arr4)
</script>
</head>
<body>
</body>
</html>
23. ES6中数组对象新增的方法
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script type="text/javascript">
var arr = [10, 20, 30, 50, 50];
var b = arr.every(function(ele) {
return ele % 2 == 0;
});
alert(b);
var arr2 = [10, 20, 30, 50, 60, 4, 3, 1, 7, 8, 11, 13, 52];
var newArr=arr2.filter(function(ele){
return ele%2==0;
});
alert(newArr);
var arr3 = ["张三","李四","王五","王老虎","王百万"];
var name=arr3.find(function(ele){
return ele.startsWith('王');
});
alert(name);
arr2.forEach(function(ele,index){
console.log(index,ele);
})
var arr3 = [10, 10,20, 30, 50, 50];
var index=arr3.indexOf(100);
alert(index);
</script>
</head>
<body>
</body>
</html>
24. ES6中数组新增的方法
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script type="text/javascript">
var arr = [10, 10,20, 30, 50, 50];
var b=Array.isArray(arr);
alert(b);
alert(arr instanceof Array);
var arr2 = ["abc","hello","index","hehe","haha"];
var newArr=arr2.map(function(ele){
return ele.toUpperCase();
});
console.log(newArr);
var numbers = [65, 44, 12, 4,10];
var sum=numbers.reduce(function(total, num,index,arr) {
console.log("元素索引",index)
return total + num;
},10);
alert(sum);
</script>
</head>
<body>
</body>
</html>
25. 正则表达式对象
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script type="text/javascript">
var regx=new RegExp("[a-b]+","i");
var flag=regx.test("ABC");
alert(flag);
var regx2=/[a-b]+/i ;
var flag2=regx2.test("ABC");
alert(flag2);
</script>
</head>
<body>
</body>
</html>
26. Js中正则表达式
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script type="text/javascript">
var str = "hello123";
var reg = /^[A-Z0-9]+$/;
alert("匹配吗?" + reg.test(str));
</script>
</head>
<body>
</body>
</html>
27. 根据正则截取字符串
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script type="text/javascript">
var str = "da jia YAO hao hao xue xi a";
var reg = /\b[a-z]{3}\b/gi;
var s = reg.exec(str);
alert(s);
s = reg.exec(str);
alert(s);
s = reg.exec(str);
alert(s);
</script>
</head>
<body>
</body>
</html>
28. 字符串对象关于正则的方法
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script type="text/javascript">
var str="aaa=bbb=ccc=ddd=eee=AAA";
var newStr=str.replace(/=/g,"");
alert(newStr);
</script>
</head>
<body>
</body>
</html>
29. Boolean对象
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script type="text/javascript">
var flag=Boolean("true");
if(flag){
alert(flag);
}
</script>
</head>
<body>
</body>
</html>
|