jQuery中文开发文档
鼠标事件
-
click()
:单击事件,鼠标按下再弹起,触发绑定的函数 -
dblclick()
:双击事件,鼠标连续双击触发事件 -
mousedown()/mouseup()
:鼠标按下或松开事件 -
mouseover()/mouseout()
:鼠标移入或移出事件,移动到元素后代也会触发 -
mouseenter()/mouseleave()
:鼠标移入或移出事件,移动到元素后代不会触发 -
mousemove()
:鼠标在元素范围内移动事件(不断触发) -
hover(fn1, fn2)
:同时绑定mouseenter
和mouseleave
事件,分别当鼠标指针进入和离开元素时被执行
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>jQuery选择器_属性选择器</title>
<script src="./jquery-3.4.1/jquery-3.4.1.min.js"></script>
<style type="text/css">
*{
margin: 0; padding: 0;}
#box{
width: 200px; height: 200px; border: 2px solid black;
margin: 50px auto;
}
#box p{
width: 100px; height: 100px; background: blue;
margin: 50px auto;
}
</style>
<script>
$(function(){
$(function(){
// 鼠标单击事件
// $("#box").click(function(){
// $("#box p").css("background", "red");
// })
// 鼠标双击事件
// $("#box").dblclick(function(){
// $("#box p").css("background", "red");
// })
// 鼠标按下事件
// $("#box").mousedown(function(){
// $("#box p").css("background", "red");
// })
// 鼠标松开事件
// $("#box").mouseup(function(){
// $("#box p").css("background", "red");
// })
// 鼠标移入事件
// var count = 0
// $("#box").mouseover(function(){
// count++;
// console.log("鼠标移入" + count);
// })
// 鼠标移出事件
// var count = 0
// $("#box").mouseout(function(){
// count++;
// console.log("鼠标移出" + count);
// })
// 鼠标移入事件
// var count = 0
// $("#box").mouseenter(function(){
// count++;
// console.log("鼠标移入" + count);
// })
// 鼠标移出事件
// var count = 0
// $("#box").mouseleave(function(){
// count++;
// console.log("鼠标移出" + count);
// })
// 鼠标在元素范围内移动事件(不断触发)
// var count = 0
// $("#box").mousemove(function(){
// count++;
// console.log("不断触发" + count);
// })
// 同时绑定 mouseenter和 mouseleave事件
$("#box").hover(function(){
$(this).css("background", "yellow");
}, function(){
$(this).css("background", "orange");
})
})
})
</script>
</head>
<body>
<div id="box">
<p></p>
</div>
</body>
</html>
键盘事件
-
keydown()
:按键按下 -
keyup()
:按键弹起 -
keypress()
:按键被按下到松开“整个过程”中触发的事件
keypress事件只在按下键盘的任一“字符键”(如A ~ Z、数字键)时触发,单独按下“功能键”(如F1~F12, Ctrl、Alt、shift等’)不会触发;而keydown无论是按下“字符键”还是“功能键”都会触发 -
event.which
:指示按了哪个键或按钮
三者顺序:keydown–>keypress–>keyup
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>事件处理</title>
<script src="./jquery-3.4.1/jquery-3.4.1.min.js"></script>
<script>
$(function(){
// 键盘事件主要用于输入时
// 1、keydown:按键按下
// $("input").keydown(function(){
// $("input").css("background", "#ccc");
// });
// 2、keyup:按键弹起
// $("input").keyup(function(){
// $("input").css("background", "#fff");
// });
// 3、事件监听,绑定不同事件,执行不同方法
// $("input").on({
// keydown:function(){
// $("input").css("background", "#ccc");
// },
// keyup:function(){
// $("input").css("background", "#fff");
// }
// })
// 4、指定按键触发事件
// keydown按键按下时,有keyCode按键编码属性
$("input").keydown(function(event){
console.log(event.keyCode);
if(event.keyCode == 13){
$("input").css("background", "#ccc");
}
});
})
</script>
</head>
<body>
姓名:<input type="text" >
</body>
</html>
触摸事件
触摸事件主要用于移动端;触摸事件应使用事件监听的方法
-
touchstart
:当手指触摸元素时,相当于PC端鼠标按下 -
touchend
:当手指离开元素时,相当于PC端的鼠标松开 -
touchmove
:当手指在元素上移动时,不断触发,相当于PC端的鼠标按下移动
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no"/>
<title>事件处理</title>
<script src="./jquery-3.4.1/jquery-3.4.1.min.js"></script>
<style type="text/css">
*{
margin: 0; padding: 0;}
#box{
width: 200px; height: 200px; border: 2px solid black;
margin: 50px auto;
}
button{
width: 100px; height: 50px; background: #ccc;;
margin: 0 auto; display: block;
}
</style>
<script>