一、操作DOM
<!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>Document</title>
</head>
<body>
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
</ul>
<script>
const oUl = document.querySelector('ul');
const oLi = document.querySelectorAll('ul>li')[0];
const oLi1 = document.querySelectorAll('ul>li')[1];
var oli5 = document.createElement('li');
oli5.innerHTML = '555';
oUl.appendChild(oli5);
oUl.insertBefore(oli5 , oUl.firstElementChild);
oUl.insertBefore(oli5 , null);
oUl.removeChild()
oUl.removeChild(oUl.firstElementChild);
</script>
</body>
</html>
二、克隆DOM
<!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>Document</title>
</head>
<body>
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
</ul>
<script>
const oUl = document.querySelector('ul');
const oLi = document.querySelector('ul>li');
console.log(oUl);
console.log(oLi);
var cloneLi = oLi.cloneNode(true);
oUl.appendChild(cloneLi)
console.log(cloneLi);
</script>
</body>
</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>Document</title>
<style>
*{
margin: 0;
padding: 0;
}
.box1{
width: 300px;
height: 300px;
background-color: yellow;
margin-top: 50px;
margin-left: 50px;
}
.box2{
width: 150px;
height: 150px;
background-color: orange;
position: absolute;
top: 102px;
left: 100px;
}
</style>
</head>
<body>
<div class="box1">
<div class="box2"></div>
</div>
<script>
const oBox1 = document.querySelector('.box1');
const oBox2 = document.querySelector('.box2');
console.log(oBox2.offsetParent);
console.log(oBox2.offsetLeft);
console.log(oBox2.offsetTop);
</script>
</body>
</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>Document</title>
<style>
*{
margin: 0;
padding: 0;
}
div{
width: 300px;
height: 300px;
background-color: pink;
padding: 20px;
border: 10px solid red;
}
</style>
</head>
<body>
<div></div>
<script>
const oDiv = document.querySelector('div');
console.log(oDiv);
console.log(oDiv.offsetWidth);
console.log(oDiv.offsetHeight);
console.log(oDiv.clientWidth);
console.log(oDiv.clientHeight);
console.log('window.innerWidth:' , window.innerWidth);
console.log('window.innerHeight:' , window.innerHeight);
console.log('document.documentElement.clientWidth' , document.documentElement.clientWidth);
console.log('document.documentElement.clientHeigth' , document.documentElement.clientHeight);
</script>
</body>
</html>
五、JS事件
<!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>Document</title>
<style>
*{
margin: 0;
padding: 0;
}
.box,
.box1
{
width: 200px;
height: 200px;
background-color: orange;
margin-bottom: 20px;
}
</style>
</head>
<body>
<div class="box"></div>
<div class="box1"></div>
<script>
const oBox = document.querySelector('.box');
oBox.onclick = function(){
console.log(1111);
}
oBox.onclick = function(){
console.log(2222);
}
const oBox1 = document.querySelector('.box1');
oBox1.addEventListener('click' , function(){
console.log(3333);
})
oBox1.addEventListener('click' , function(){
console.log(4444);
})
</script>
</body>
</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>Document</title>
</head>
<body>
<div>1111</div>
<script>
window.onload = function(){
var oDiv = document.querySelector('div');
console.log(oDiv);
}
</script>
</body>
</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>Document</title>
<style>
*{
margin: 0;
padding: 0;
}
.box1{
width: 200px;
height: 200px;
background-color: yellow;
}
p{
width: 100px;
height: 100px;
background-color: orange;
}
</style>
</head>
<body>
<div class="box1">
<p>羽生</p>
</div>
<script>
const oBox1 = document.querySelector('.box1');
oBox1.addEventListener('click' , function(){
console.log('鼠标单击事件');
})
oBox1.addEventListener('mouseenter' , function(){
console.log('鼠标移入事件1');
})
oBox1.addEventListener('mouseover' , function(){
console.log('鼠标移入事件2');
})
</script>
</body>
</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>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
div {
width: 200px;
height: 200px;
background-color: yellow;
}
p {
width: 100px;
height: 100px;
background-color: orange;
}
</style>
</head>
<body>
<div></div>
<script>
const oDiv = document.querySelector('div');
document.onkeydown = function(){
console.log('键盘按下事件');
}
document.onkeyup = function(){
console.log('键盘抬起事件');
}
document.onkeypress = function(){
console.log('键盘按下抬起事件');
}
</script>
</body>
</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>Document</title>
</head>
<body>
输入框:<input type="text" id="ipt">
<script>
const oIpt = document.querySelector('#ipt');
oIpt.onfocus = function(){
console.log('获取焦点');
}
oIpt.onblur = function(){
console.log('失去焦点');
}
oIpt.onchange = function(){
console.log('改变内容');
}
oIpt.oninput = function(){
console.log('输入内容');
}
</script>
</body>
</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>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
div {
width: 200px;
height: 200px;
background-color: yellow;
}
</style>
</head>
<body>
<div></div>
<script>
const oDiv = document.querySelector('div');
oDiv.onclick = function(event){
console.log(event);
}
</script>
</body>
</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>Document</title>
<style>
*{
margin: 0;
padding: 0;
}
div{
width: 200px;
height: 200px;
background-color: red;
position: fixed;
margin-top: 50px;
}
body{
height: 5000px;
}
</style>
</head>
<body>
<div></div>
<script>
var oDiv = document.querySelector('div');
oDiv.onclick = function(event){
console.log('相对于事件源X轴的位置:' , event.offsetX);
console.log('相对于事件源Y轴的位置:' , event.offsetY);
console.log('获取相对于页面X轴的坐标点:' , event.pageX);
console.log('获取相对于页面Y轴的坐标点:' , event.pageY);
console.log('获取相对于浏览器窗口的X轴:' , event.clientX);
console.log('获取相对于浏览器窗口的Y轴:' , event.clientY);
}
</script>
</body>
</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>Document</title>
</head>
<body>
<input type="text" class="ipt">
<script>
const oIpt = document.querySelector('.ipt');
oIpt.onkeyup = function(event){
console.log(event.key);
}
</script>
</body>
</html>