1.引入方式
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JS-引入方式</title>
</head>
<body>
<!-- 建议js放在body的最后 -->
<!-- 内部引入 -->
<script>
alert("hello js");
</script>
<!-- 外部引入 -->
<script src="js/demo.js"></script>
</body>
</html>
2.变量和常量
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JS-基础语法</title>
</head>
<body>
<script>
// 变量let,弱类型
let a = 20;
a = "hello";
alert(a)
// 常量const,不能重新赋值
const b = 'js';
// b = 'java';
console.log(b);
// 输出内容到body中
document.write(b);
</script>
</body>
</html>
3.数据类型
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JS-数据类型</title>
</head>
<body>
<script>
// 1.数据类型
//number
alert(typeof 10);
alert(typeof 1.5);
alert(typeof NaN);
//boolean
alert(typeof true);
alert(typeof false);
//string
alert(typeof "Hello");
alert(typeof 'JS');
alert(typeof `JavaScript`);
//null
alert(typeof null);
//undefined
let a ;
alert(typeof a);
// 2.模板字符串,用${}而不是+拼接
let name = 'tom';
let age = 18;
console.log('我叫'+name+',今年'+age);
console.log(`我叫${name},今年${age}`);
</script>
</body>
</html>
4.函数
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JS-函数</title>
</head>
<body>
<script>
// 1.具名函数
function add(a, b){
return a + b;
}
let c = add(10, 20);
console.log(c);
// 2.匿名函数
// 2.1函数表达式
let sub = function(a, b){
return a - b;
}
// 2.2箭头函数
let div = (a, b) => {
return a / b;
}
</script>
</body>
</html>
5.自定义对象、json
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JS-自定义对象</title>
</head>
<body>
<script>
// 1.自定义对象
let user = {
name: 'tom',
age: 18,
say_hi(msg) {
alert(`我叫${this.name},今年${this.age},${msg}`)
}
// say_hi: function (msg) {
// alert(`我叫${this.name},今年${this.age},${msg}`)
// }
}
user.say_hi('我喜欢唱歌');
// 2.json
// 2.1对象转为json
let p = {
name: 'alice',
gender: 'male'
};
alert(JSON.stringify(p));
// 2.2json转为对象,json串必须都用双引号
let pj = `{
"name": "alice",
"gender": "male"
}`;
alert(JSON.parse(pj).name);
</script>
</body>
</html>
6.DOM
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JS-DOM</title>
</head>
<body>
<h1 id="title1">11111</h1>
<h1>22222</h1>
<h1>33333</h1>
<script>
// DOM文档对象模型包括:
// Document整个文档
// Element标签元素
// Attribute标签属性
// Text标签文本
// Comment
// let h1 = document.querySelector('#title1');
let h1 = document.querySelector('h1');
let h1s = document.querySelectorAll('h1');
h1.innerHTML='我被修改了';
h1s[2].innerHTML='我被修改了';
</script>
</body>
</html>
7.事件监听
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JS-事件-常见事件</title>
</head>
<body>
<form action="" style="text-align: center;">
<input type="text" name="username" id="username">
<input type="text" name="age" id="age">
<input id="b1" type="submit" value="提交">
<input id="b2" type="button" value="单击事件">
</form>
<br><br><br>
<table width="800px" border="1" cellspacing="0" align="center">
<tr>
<th>学号</th>
<th>姓名</th>
<th>分数</th>
<th>评语</th>
</tr>
<tr align="center">
<td>001</td>
<td>张三</td>
<td>90</td>
<td>很优秀</td>
</tr>
<tr align="center" id="last">
<td>002</td>
<td>李四</td>
<td>92</td>
<td>优秀</td>
</tr>
</table>
<script>
//click: 鼠标点击事件
document.querySelector('#b2').addEventListener('click', () => {
console.log("我被点击了...");
})
//mouseenter: 鼠标移入
document.querySelector('#last').addEventListener('mouseenter', () => {
console.log("鼠标移入了...");
})
//mouseleave: 鼠标移出
document.querySelector('#last').addEventListener('mouseleave', () => {
console.log("鼠标移出了...");
})
//keydown: 某个键盘的键被按下
document.querySelector('#username').addEventListener('keydown', () => {
console.log("键盘被按下了...");
})
//keyup: 某个键盘的键被抬起
document.querySelector('#username').addEventListener('keyup', () => {
console.log("键盘被抬起了...");
})
//blur: 失去焦点事件
document.querySelector('#age').addEventListener('blur', () => {
console.log("失去焦点...");
})
//focus: 元素获得焦点
document.querySelector('#age').addEventListener('focus', () => {
console.log("获得焦点...");
})
//input: 用户输入时触发
document.querySelector('#age').addEventListener('input', () => {
console.log("用户输入时触发...");
})
//submit: 提交表单事件
document.querySelector('form').addEventListener('submit', () => {
alert("表单被提交了...");
})
</script>
</body>
</html>
8.js文件之间的引入
demo,js
javascript
export function printLog(msg){
console.log(msg);
}
demo1.js
javascript
import { printLog } from './js/demo.js'
//click: 鼠标点击事件
document.querySelector('#b2').addEventListener('click', () => {
printLog("我被点击了...");
})
//mouseenter: 鼠标移入
document.querySelector('#last').addEventListener('mouseenter', () => {
printLog("鼠标移入了...");
})
demo.html
html
<script src="./js/demo1.js" type="module"></script>