什么是事件?页面对不同访问者的响应叫做事件。事件处理程序指的是当 HTML 中发生某些事件时所调用的方法。
常用的时间主要有以下几种:
click()事件:click() 方法是当按钮点击事件被触发时会调用一个函数。该函数在用户点击 HTML 元素时执行;
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>click()事件</title>
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("p").click(function(){
$(this).hide();
});
});
</script>
</head>
<body>
<p>如果你点我,我就会消失。</p>
<p>点我消失!</p>
<p>点我也消失!</p>
</body>
</html>
dblclick()事件:当双击元素时,会发生 dblclick 事件。dblclick() 方法触发 dblclick 事件,或规定当发生 dblclick 事件时运行的函数;
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>dblclick()事件</title>
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("p").dblclick(function(){
$(this).hide();
});
});
</script>
</head>
<body>
<p>双击鼠标左键的,我就消失。</p>
<p>双击我消失!</p>
<p>双击我也消失!</p>
</body>
</html>
mouseenter()事件:当鼠标指针穿过元素时,会发生的事件。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>mouseenter()事件</title>
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("#p1").mouseenter(function(){
alert('您的鼠标移到了 id="p1" 的元素上!');
});
});
</script>
</head>
<body>
<p id="p1">鼠标指针进入此处,会看到弹窗。</p>
</body>
</html>
mouseleave()事件:当鼠标指针离开元素时,会发生 的事件。
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>mouseleave()事件</title> <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"> </script> <script> $(document).ready(function(){ $("#p1").mouseleave(function(){ alert("再见,您的鼠标离开了该段落。"); }); }); </script> </head> <body> <p id="p1">这是一个段落。</p> </body> </html>
mousedown()事件:当鼠标指针移动到元素上方,并按下鼠标按键时,会发生的事件。
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>mousedown()事件</title> <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"> </script> <script> $(document).ready(function(){ $("#p1").mousedown(function(){ alert("鼠标在该段落上按下!"); }); }); </script> </head> <body> <p id="p1">这是一个段落</p> </body> </html>
mouseup()事件:当在元素上松开鼠标按钮时,会发生的事件。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>mouseup()事件</title>
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("#p1").mouseup(function(){
alert("鼠标在段落上松开。");
});
});
</script>
</head>
<body>
<p id="p1">这是一个段落。</p>
</body>
</html>
hover()事件:用于模拟光标悬停事件。
<!DOCTYPE html> <html> <head> <meta charset="utf-8">
<title>hover()事件</title> <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"> </script> <script> $(document).ready(function(){ $("#p1").hover( function(){ alert("你进入了 p1!"); }, function(){ alert("拜拜! 现在你离开了 p1!"); } ) }); </script> </head> <body> <p id="p1">这是一个段落。</p> </body> </html>
focus()事件:当元素获得焦点时,发生的事件。当通过鼠标点击选中元素或通过 tab 键定位到元素时,该元素就会获得焦点。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>focus()事件</title>
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("input").focus(function(){
$(this).css("background-color","#cccccc");
});
$("input").blur(function(){
$(this).css("background-color","#ffffff");
});
});
</script>
</head>
<body>
Name: <input type="text" name="fullname"><br>
Email: <input type="text" name="email">
</body>
</html>
blur()事件:当元素失去焦点时,发生 blur 事件。blur() 方法触发 blur 事件,或规定当发生 blur 事件时运行的函数。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>blur()事件</title>
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("input").focus(function(){
$(this).css("background-color","#cccccc");
});
$("input").blur(function(){
$(this).css("background-color","#ffffff");
});
});
</script>
</head>
<body>
Name: <input type="text" name="fullname"><br>
Email: <input type="text" name="email">
</body>
</html>
事件处理程序指的是当 HTML 中发生某些事件时所调用的方法,是 jQuery 中的核心函数。