文章目录
学习总结:
JavaScript引入方式:
内部脚本:
外部脚本:
案例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<!--<script>
alert("hello JavaScript!");
</script>-->
<script src="../js/test.js"></script>
</body>
</html>
test.jsp代码:
alert("hello JavaScript!");
运行结果:
JavaScript基础语法:
书写语法:
输出语句:
案例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script>
window.alert("马奎斯");
document.write("罗西");
console.log("Hello console!");
</script>
</body>
</html>
这个是写入浏览器控制台的运行截图:
变量:
案例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script>
</script>
</body>
</html>
数据类型
案例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script>
var char='a';
var name='小马哥';
var users="马奎斯";
alert(typeof char);
alert(typeof name);
alert(typeof users);
var a=12;
var b=12.12;
alert(typeof a);
alert(typeof b);
var n=null;
alert(typeof n);
var b1=true;
var b2=false;
alert(typeof b1);
alert(typeof b2);
var un;
alert(typeof un);
</script>
</body>
</html>
运算符:
案例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script>
var flag=undefined;
if (flag){
alert("转换为true")
}
else {
alert("转换为false")
}
</script>
</body>
</html>
流程控制语句:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script>
var sum=0;
var i=0;
do {
sum+=i;
i++;
}while (i<=100){
alert(sum);
}
</script>
</body>
</html>
函数:
案例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script>
var add=function (a,b) {
return a*b;
}
var result=add(10,10,10,10);
alert(result);
</script>
</body>
</html>
JavaScript常用对象:
Array对象:
案例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script>
var arr=[1,2,3,4,5];
arr.splice(0,3);
alert(arr);
</script>
</body>
</html>
String:
案例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script>
var str=new String("abc");
var str1="abc";
var str2='abc';
alert(str.length);
var str3=" abc ";
alert(0+str3+10);
alert(0+str3.trim()+10);
</script>
</body>
</html>
自定义对象:
案例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script>
var person={
name:"小明",
age:12,
sleep:function () {
alert("睡觉!")
}
};
alert(person.name);
alert(person.age);
person.sleep();
</script>
</body>
</html>
BOM:
windows:浏览窗口对象
案例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script>
window.alert("abc");
alert("abc");
let b = confirm("您确定要删除吗?");
alert(b);
if (b){
}
setInterval(function () {
alert("间隔三秒循环执行")
},3000)
setTimeout(function () {
alert("间隔三秒执行一次")
},3000)
</script>
</body>
</html>
定时器案例:
开灯和关灯循环切换:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<input type="button" onclick="on()" value="开灯">
<img id="myImage" border="0" src="../images/off.gif" style="...">
<input type="button" onclick="off()" value="关灯">
<script>
function on() {
document.getElementById('myImage').src='../images/on.gif';
}
function off() {
document.getElementById('myImage').src='../images/off.gif';
}
var x=0;
setInterval(function () {
if (x%2==0){
on();
}else {
off();
}
x++;
},1000)
</script>
</body>
</html>
运行将结果:开灯和关灯隔一秒循环切换。
history:历史记录对象
location:地址栏对象
案例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script>
alert("跳转到百度");
location.href="https://www.baidu.com";
</script>
</body>
</html>
运行结果: 点击确定后跳转:
DOM:
获取Element对象
事件监听:
事件绑定:
案例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<input type="button" value="点击" onclick="on()">
<br>
<input type="button" value="再次点击" id="ok">
<script>
function on() {
alert("被点击了")
}
document.getElementById("ok").onclick=function () {
alert("再次被点击了")
}
</script>
</body>
</html>
运行结果:
常见事件:
正则表达式
案例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script>
var reg=/^\w{3,6}$/;
var str="abc";
let b = reg.test(str);
alert(b);
</script>
</body>
</html>
|