1.事件函数列表
在延寿等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供成都网站设计、成都网站建设 网站设计制作按需网站开发,公司网站建设,企业网站建设,品牌网站设计,全网营销推广,外贸营销网站建设,延寿网站建设费用合理。
(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.绑定事件的其他方式
绑定事件
$(function(){
/*click事件
$('#btn').click(function(){
alert('click')
})
*/
/*移入和点击都触发*/
$('#btn').bind('mouseover click',function(){
alert('bind');
/*第二次进入解绑*/
$(this).unbind('mouseover');
})
})
3.事件冒泡
在一个对象上触发某类事件(比如单击onclick),如果此对象定义了此事件的处理程序,那么此事件就会调用这个处理程序,如果没有定义此事件处理程序或者事件返回true,那么这个事件会向这个对象的父级对象传播,从里到外,直至它被处理(父级对象所有同类事件都将被激活),或者它达到了对象层次的最顶层,即document对象(有些浏览器是window)
4.事件冒泡的作用
事件冒泡允许多个操作被集中处理(把事件处理添加到一个父级元素上,避免把事件处理器添加到多个子级元素上),它还可以让你在对象层的不同级别捕获事件。
5.阻止事件冒泡
事件冒泡机制有时候是不需要的,需要阻止掉,通过event.stopPropagation()来阻止
事件冒泡
.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;
}
6.阻止默认行为
阻止表单提交
7.合并阻止
一般把阻止冒泡和阻止默认行为合并起来写,合并写法可以用
$('.grandfather').click(function(){
alert(3);
/第二种阻止事件写法,合并写法/
return false;
})
例子:弹框
弹框
$(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();
})
})
.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;
}
弹框文字
输入颜值:
x

8.事件委托
事件委托就是利用冒泡的原理,把事件加到父级上,通过判断事件来源的子集,执行相应的操作,事件委托首先可以极大减少事件绑定次数,提高性能,其次可以让新加入的子元素也可以拥有相同的操作
事件委托
$(function(){
//普通写法,性能不高,新加入的li需要重新绑定
$('.list li').click(function(){
$(this).css({'backgroundColor':'red'});
})
/*
var $li = $('9 ')
$('.list').append($li);
*/
/*重新绑定,$li.click(....)*/
/*事件委托,父级list受委托,li的委托,click事件,函数*/
$('.list').delegate('li','click',function(){
$(this).css({'backgroundColor':'red'});
})
/*事件委托不用重新绑定*/
var $li = $('9 ')
$('.list').append($li);
})
.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;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8

网页名称:jQuery事件
标题网址:http://kswsj.com/article/gieeih.html
Copyright © 2009-2022 www.kswsj.com 成都快上网科技有限公司 版权所有 蜀ICP备19037934号