(1)click鼠标事件
(2)mouseover() 鼠标进入(进入子元素也触发)
(3)mouseout() 鼠标离开(离开子元素也触发)
(4)mouseenter()鼠标进入(进入子元素不触发)
(5)mouseleave()鼠标离开(离开子元素不触发)
(6)hover()
$(function(){
???/*移入,子元素也会触发*/ ???/*$(‘.box1‘).mouseover(function(){ ???????alert(‘移入‘); ???})*/ ???/*移出,子元素也会触发*/ ???/*$(‘.box1‘).mouseout(function(){ ???????alert(‘移出‘); ???})*/ ???/*移入移出,子元素不会触发,hover是合并写法*/ ???$(‘.box1‘).mouseenter(function(){ ???????alert(‘移入‘); ???}) ???$(‘.box1‘).mouseleave(function(){ ???????alert(‘移出‘); ???})})
(7)ready()DOM加载完成
(8)resize()浏览器窗口的大小发生改变
$(window).resize(function(){
var $wr = $(window).width();
document.title = $wr;
})
(9)scroll()滚动条的位置发生变化
(10)submit()用户递交表单
$(function(){
/一开始就获得焦点,元素只能一个获得焦点,blur失去焦点/
$(‘#ipt1‘).focus();
/$(‘#ipt2‘).focus();/
$(‘#fm1‘).submit(function(){
/alert(‘提交‘);/
/拒绝提交/
return false;
})
})
(11)blur()元素失去焦点
(12)focus()元素获得焦点
$(function(){
/一开始就获得焦点,元素只能一个获得焦点,blur失去焦点/
$(‘#ipt1‘).focus();
/$(‘#ipt2‘).focus();/
})
2.绑定事件的其他方式
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>绑定事件</title>
<script type="text/javascript" src="../jQuery库/jquery-3.3.1.min.js"></script>
<script type="text/javascript">
$(function(){ ???/*click事件 ???$(‘#btn‘).click(function(){ ???????alert(‘click‘) ???}) ???*/ ???/*移入和点击都触发*/ ???$(‘#btn‘).bind(‘mouseover click‘,function(){ ???????alert(‘bind‘); ???????/*第二次进入解绑*/ ???????$(this).unbind(‘mouseover‘); ???})})</script>
</head>
<body>
<input type="button" value="进入" id="btn">
</body>
</html>
3.事件冒泡
在一个对象上触发某类事件(比如单击onclick),如果此对象定义了此事件的处理程序,那么此事件就会调用这个处理程序,如果没有定义此事件处理程序或者事件返回true,那么这个事件会向这个对象的父级对象传播,从里到外,直至它被处理(父级对象所有同类事件都将被激活),或者它达到了对象层次的最顶层,即document对象(有些浏览器是window)
4.事件冒泡的作用
事件冒泡允许多个操作被集中处理(把事件处理添加到一个父级元素上,避免把事件处理器添加到多个子级元素上),它还可以让你在对象层的不同级别捕获事件。
5.阻止事件冒泡
事件冒泡机制有时候是不需要的,需要阻止掉,通过event.stopPropagation()来阻止
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>事件冒泡</title>
<script type="text/javascript" src="../jQuery库/jquery-3.3.1.min.js"></script>
<script type="text/javascript">
$(function(){
???/*事件往上传,弹出123,a表示事件对象,一般写event*/ ???$(‘.son‘).click(function(a){ ???????alert(1); ???????/*阻止事件冒泡*/ ???????a.stopPropagation(); ???}) ???/*弹出23*/ ???$(‘.father‘).click(function(){ ???????alert(2); ???}) ???/*弹出3*/ ???$(‘.grandfather‘).click(function(){ ???????alert(3); ???????/*第二种阻止事件写法*/ ???????return false; ???}) ???/*整个文档最顶级*/ ???$(document).click(function(){ ???????alert(4); ???})})</script>
<style type="text/css">
.grandfather{ ???width: 300px; ???height: 300px; ???background-color: antiquewhite; ???cursor: pointer;}.father{ ???width: 200px; ???height: 200px; ???background-color: indianred; ???cursor: pointer;}.son{ ???width: 100px; ???height: 100px; ???background-color: tan; ???cursor: pointer;}</style>
</head>
<body>
<div class="grandfather"> ???<div class="father"> ???????<div class="son"> ???????</div> ???</div></div>
</body>
</html>
6.阻止默认行为
阻止表单提交
7.合并阻止
一般把阻止冒泡和阻止默认行为合并起来写,合并写法可以用
$(‘.grandfather‘).click(function(){
alert(3);
/第二种阻止事件写法,合并写法/
return false;
})
例子:弹框
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>弹框</title>
<script type="text/javascript" src="../jQuery库/jquery-3.3.1.min.js"></script>
<script type="text/javascript">
$(function(){ ???$(‘.btn‘).click(function(){ ???????$(‘.pop_con‘).fadeIn(); ???????return false; ???}) ???$(document).click(function(){ ???????$(‘.pop_con‘).fadeOut(); ???}) ???$(‘.pop1‘).click(function(){ ???????return false; ???}) ???$(‘#off‘).click(function(){ ???????$(‘.pop_con‘).fadeOut(); ???})})</script>
<style type="text/css">
.pop_con{ ???display: none;}.pop1{ ???width: 300px; ???height: 300px; ???border: 3px solid #000; ???background-color: #87CEF5; ???position: fixed; ???left: 50%; ???top: 50%; ???margin-left: -150px; ???margin-top: -150px; ???z-index: 100;}.pop2{ ???width: 100%; ???height: 100%; ???background-color: #000000; ???opacity: 0.3; ???filter: alpha(opacity=30); ???position: fixed; ???left: 0; ???top: 0; ???z-index: 98;}.close{ ???font-size: 30px; ???text-decoration: none; ???color: red; ???float: right;}</style>
</head>
<body>
<input type="button" value="点击" class="btn"><div class="pop_con"> ???<div class="pop1"> ???????弹框文字 ???????输入颜值: ???????<input type="text" name="" id="b01"> ???????<a href="#" class="close" id="off">x</a> ???</div> ???<div class="pop2"></div></div>
</body>
</html>
8.事件委托
事件委托就是利用冒泡的原理,把事件加到父级上,通过判断事件来源的子集,执行相应的操作,事件委托首先可以极大减少事件绑定次数,提高性能,其次可以让新加入的子元素也可以拥有相同的操作
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>事件委托</title>
<script type="text/javascript" src="../jQuery库/jquery-3.3.1.min.js"></script>
<script type="text/javascript">
$(function(){ ???//普通写法,性能不高,新加入的li需要重新绑定 ???$(‘.list li‘).click(function(){ ???????$(this).css({‘backgroundColor‘:‘red‘}); ???}) ???/* ???var $li = $(‘<li>9</li>‘) ???$(‘.list‘).append($li); ???*/ ???/*重新绑定,$li.click(....)*/ ???/*事件委托,父级list受委托,li的委托,click事件,函数*/ ???$(‘.list‘).delegate(‘li‘,‘click‘,function(){ ???????$(this).css({‘backgroundColor‘:‘red‘}); ???}) ???/*事件委托不用重新绑定*/ ???var $li = $(‘<li>9</li>‘) ???$(‘.list‘).append($li);})</script>
<style type="text/css">
.list{ ???width: 500px; ???height: 400px; ???background-color: antiquewhite; ???text-align: center; ???list-style: none; ???padding: 0;}.list li{ ???width: 500px; ???height: 40px; ???background-color: aquamarine; ???margin: 10px auto;}</style>
</head>
<body>
<ul class="list"> ???<li>1</li> ???<li>2</li> ???<li>3</li> ???<li>4</li> ???<li>5</li> ???<li>6</li> ???<li>7</li> ???<li>8</li></ul>
</body>
</html>
jQuery事件
原文地址:http://blog.51cto.com/13742773/2341811