jQuery事件
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://www.xueling.net.cn/article/gieeih.html