鼠标事件
<!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>
.box1{
width: 100px;
height: 100px;
background-color: red;
}
</style>
</head>
<body>
<button id="btn1">单击</button>
<button id="btn2">双击</button>
<button id="btn3">鼠标按下</button>
<button id="btn4">鼠标抬起</button>
<div id="btn5" class="box1"></div>
<script>
var btn1=document.getElementById("btn1");
var btn2=document.getElementById("btn2");
var btn3=document.getElementById("btn3");
var btn4=document.getElementById("btn4");
btn1.onclick = function(){
console.log("单击事件");
}
btn2.ondblclick = function(){
console.log("双击事件");
}
btn3.onmousedown = function(){
console.log("鼠标按下");
}
btn4.onmouseup = function(){
console.log("鼠标抬起");
}
btn5.onmousemove = function(){
console.log("鼠标移动了");
}
btn5.onmouseenter=function(){
console.log("鼠标进入事件");
}
btn5.onmouseleave=function(){
console.log("鼠标离开事件");
}
btn5.onmouseover=function(){
console.log("鼠标over事件");
}
btn5.onmouseout=function(){
console.log("鼠标out事件");
}
/*
over和out的区别
enter和leave的区别
over和out进入和离开子节点的时候会触发
*/
</script>
</body>
</html>
dem0事件
<!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>
<button id="btn">按钮</button>
<script>
//优点,分离js和html
//缺点,会覆盖第一个
var tt=document.getElementById("btn")
tt.onclick=function()
{
console.log('点击了按钮1');
}
tt.onclick=function()
{
console.log('点击了按钮2');
}
</script>
</body>
</html>
dem2事件
<!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>
<button id="but">按钮</button>
<script>
var bt=document.getElementById("but");
//这里的click是代表事件类型
//优点,可以触发多个东西
//缺点写起来麻烦
bt.addEventListener("click",function(){
console.log("点击了1");
})
bt.addEventListener("click",function(){
console.log("点击了2");
})
</script>
</body>
</html>
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>
<button onclick="fun()">按钮</button>
<script>
//html事件缺点,html和js没有分开,html里面有js的代码
function fun()
{
console.log("点击了按钮");
}
</script>
</body>
</html>
event事件
<!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>
.box1{
width: 200px;
height: 200px;
background-color: red;
}
.box2{
width: 100px;
height: 100px;
background-color: blue;
}
</style>
</head>
<body>
<button id="btu">按钮</button>
<a href="https://www.baidu.com" id="it">dianwo</a>
<div class="box1" id="b1"><div class="box2" id="b2"></div></div>
<script>
//事件发生后会产生一个事件对象,作为参数传给监听函数
var btn = document.getElementById("btu")
//对象属性:1.event.target(点击后改变的值) 2.event.type(知道点击的类型)
btn.onclick=function(event){
console.log(event.target);
event.target.innerHTML="点击之后"
console.log(event.type);
}
//常用的方法
//1.preventDefault,取消浏览器的默认行为
//2.stopPropagation,阻止事件冒泡,什么是元素冒泡,元素重叠的时候会点击子元素父元素也会发生!
var it=document.getElementById("it")
it.onclick=function(e){
e.preventDefault();
console.log("点击了A");
}
var b1=document.getElementById("b1")
var b2=document.getElementById("b2")
b1.onclick=function(e){
e.stopPropagation()
console.log("点击了b1");
}
b2.onclick=function(e){
console.log("点击了b2");
}
</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>
<!--
1.keydowm按下键盘时触发
2.keypress按下有值的时候触发,ctrl,alt,shift,meta这四个就是无值得键
对于有值得键,我们先触发keydown再触发这个事件
3.keyup松开得时候触发
-->
<input type="text" id="username">
<script>
var username=document.getElementById("username");
username.onkeydown=function(e){
console.log(e.target.value);
//这个value属性就是输入框得数据
}
//按下什么案件我们通过唯一标识来知道,keyCode代表每个键盘的唯一数字
username.onkeydown=function(e){
if(e.keyCode==13)
{
console.log("这是回车");
}
//这个value属性就是输入框得数据
}
</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="username">
<form id="myfrom" onsubmit="sub" action="服务器地址">
<input type="text" name="username">
<button id="resetbtn">重置</button>
<button id="tj">提交</button>
</form>
<script>
//input
var username=document.getElementById("username")
username.oninput=function(e){
console.log(e.target.value);//一边输入一边得到
}
//select选中类容的时候触发事件
username.onselect=function(e){
console.log("选中了");
}
//change事件,和input差不多,失去焦点和回车触发
//reset事件和submit事件,是直接添加到<from>对象上,而不是发生在表单的成员上
//reset再表单重置时候触发,所有表单成员变为默认值得时候触发
var res=document.getElementById("resetbtn")
var myf=document.getElementById("myfrom")
res.onclick=function()`在这里插入代码片`
{
myf.reset();
}
//submit是向服务器发数据得时候触发
function submitHandle()
{
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>
<script>
//延时执行,
var t=setTimeout(function(){
console.log("大家好");
},3000)
//注意,如果回调函数是对象的方法,那么settimeout使得方法内部的this关键字指向全局环境,而不是定义时所在的那个对象
var name=2
var user={
name :1,
getname:function(){
setTimeout(function(){
console.log(this.name);
},1000)
}
}
user.getname();
clearTimeout(t);//直接取消定时器,不会答应!!!!!!
//setinterval,每间隔多久执行一次,与上面完全一致,可以实现动画!!!
</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>
h3{
height: 400px;
}
</style>
</head>
<body>
<h3>hhh</h3>
<h3>hhh</h3>
<h3>hhh</h3>
<h3>hhh</h3>
<h3>hhh</h3>
<script>
window.onscroll=debounce(scrollHandle,2000)
function scrollHandle(){
var top=document.documentElement.scrollTop;
console.log(top);
}
//一个函数执行频率太高了我们就需要防抖,隔一段事件执行一次
/*
1.设置时间内没有再次触发事件,那么就执行这个函数
2.设置时间内再次出发滚动事件,那么就取消计时器
效果:短时间内大量执行同一函数只会执行一次
*/
function debounce(fn,delay){
var time=null
return function(){
if(time){
clearTimeout(time)
}
time=setTimeout(fn,delay)
}
}
</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>
h3{
height: 400px;
}
</style>
</head>
<body>
<h3>hhh</h3>
<h3>hhh</h3>
<h3>hhh</h3>
<h3>hhh</h3>
<h3>hhh</h3>
<script>
//为了让用户不停的动也能实现功能
function jieliu(f,delay){
var time=true;
return function(){
if(!time){
return false
}
time=false
setTimeout(function(){
f();
time=true
},delay)
}
}
window.onscroll=jieliu(scrollHandle,2000)
function scrollHandle(){
var top=document.documentElement.scrollTop;
console.log(top);
}
/*
1.搜索业务,每输入一次搜索一次————————节流
2.渲染,停下来再渲染——————————防抖
*/
</script>
</body>
</html>