鋒利的jQuery 要點歸納(三) jQuery中的事件和動畫(上:事件篇)
更新時間:2010年03月24日 17:04:13 作者:
鋒利的jQuery 要點歸納 jQuery中的事件和動畫(上:事件篇)
一、事件
1 加載DOM
$(document).ready(function(){//...})
DOM加載完畢后執(zhí)行,在可重復(fù)使用上區(qū)別于window.onload=function(){//...}
$(window).load(function(){//...})
window內(nèi)所有對象加載完畢后執(zhí)行,幾等同window.onload=function(){//...}。也可針對selector使用此方法
另:$(document).ready(function(){//...})的簡寫方式:$(function(){//...})或$().ready(function(){//...})
2 事件綁定
$("selector").bind()
為元素綁定事件,格式:bind(type[,data],fn),可多次調(diào)用
type事件類型包括:blur, focus, load, resize, scroll, unload, click, dbclick, mousedown, mouseup, mousemove, mouseover, mouseout, mouseenter, mouseleave, change, select, submit, keydown, keypress, keyup, error或者自定義事件
簡寫方法:$("selector").bind(type,function(){//...})等價于$("selector").type(function(){//...})
可傳遞data參數(shù)以供unbind特定事件之用
$("selector").is()
判斷方法
(外:方法多次重用可定義局部變量 var $x = $("selector").方法())
3 合成事件
$("selector").hover(enter,leave)
模擬光標(biāo)懸停事件,鼠標(biāo)進入時觸發(fā)enter事件,鼠標(biāo)移出時觸發(fā)leave事件(代替的是bind("mouseenter")和bind("mouseleave"))
使用方法:$("selector").hover(function(){//enter case...},function(){//leave case...})
(外:IE6不支持除a標(biāo)簽外css的:hover偽類的問題——可用此hover事件作為hack來解決)
$("selector").toggle(fn1,fn2,...,fnN)
模擬鼠標(biāo)連續(xù)單擊事件,按照單擊順序按次序循環(huán)執(zhí)行事件
使用方法:$("selector").toggle(function(){//case1...},function(){//case2...},...,function(){//caseN})
特殊用法:切換元素可見狀態(tài),如元素隱藏,單擊toggle觸發(fā)元素可使之可見;元素可見,單擊toggle觸發(fā)元素使之隱藏
P108例:
<script>
$(function(){
$("panel h5.head").toggle(function(){
$(this).next().toggle();
},function(){
$(this).next().toggle();
})
})
</script>
4 事件冒泡
$("selector").bind("type",function(event){//event:事件對象...})
event事件對象:只有此函數(shù)內(nèi)部才能訪問到,事件處理函數(shù)執(zhí)行完畢后,事件對象就被銷毀
event.stopPropagation()
在函數(shù)最后用來停止事件冒泡
P111例:
<script>
$('span').bind("click",function(event){
var txt = $('msg').html() + "<p>內(nèi)層span元素被單擊</p>";
$('#msg').html(txt);
event.stopPropagation();
})
</script>
event.preventDefault()
阻止元素默認(rèn)行為
例:驗證表單(input為空阻止提交并提示)
<script>
$(function(){
$("#submit").bind("click",function(event){
var username=$("#username").val();
if(username==""){
$("#msg").html("<p> 文本框的值不能為空</p>");
event.preventDefault();
}
});
})
</script>
return false;
同時對對象事件停止冒泡和默認(rèn)行為,等價于同時調(diào)用stopPrapagation()和preventDefault()
(外:事件捕獲和事件冒泡是相反的過程,事件捕獲是從DOM頂端往下開始觸發(fā),jQuery不支持,故本書從略)
5 事件對象的屬性
event.type
獲取事件類型
例:
<script>
$("a").click(function(event){
alert(event.type);
return false;
})
</script>
上面返回"click"
event.target
獲取觸發(fā)事件的元素
例:
<script>
$("a[href=http://baidu.com]").click(function(){
alert(event.target.href);
return false;
})
</script>
上面返回"http://baidu.com"
event.relatedTarget
訪問事件相關(guān)元素
event.pageX / event.pageY
獲取光標(biāo)相對于頁面的x坐標(biāo)和y坐標(biāo)
event.which
在鼠標(biāo)單擊事件中獲取鼠標(biāo)的左、中、右鍵;在鍵盤事件中獲取鍵盤的按鍵(返回值1=鼠標(biāo)左鍵;2=鼠標(biāo)中鍵;3=鼠標(biāo)右鍵)
event.metaKey
鍵盤事件中獲取<ctrl>按鍵
event.originalEvent
指向原始的事件對象
6 移除事件
$("selector").unbind()
移除元素上的事件,格式:$("selector").unbind([type][,data]);(如果沒有參數(shù),則刪除所有綁定的事件;如果提供了事件類型參數(shù),則只刪除該類型的綁定事件;如果把在綁定時傳遞的處理函數(shù)作為第二個參數(shù),則只有這個特定的事件處理函數(shù)會被刪除)
例:
<script>
$(function(){
$('#btn').bind("click",myFun1=function(){ //先綁定
$('#test').append("...");
});
})
</script>
<script>
$('#delOne').click(function(){
$('#btn').unbind("click",myFun1); //后刪除
})
</script>
$("selector").one()
綁定一個觸發(fā)一次即被刪除的事件,格式:$("selector").one(type[,data],fn);
7 模擬操作
$("selector").trigger("type");
模擬用戶交互動作,簡寫方法:$("#selector").type(); 格式:$("selector").trigger(type[,data])
例:用單擊替代鼠標(biāo)經(jīng)過
<script>
$("selector").mouseover(function{//...});
$("selector2").click(function(){
$("selector").trigger("mouseover"); //或者$("selector").mouseover()
})
</script>
自定義事件的例子
<script>
$("selector").bind("myClick",function(){//...}); //綁定自定義事件
$("selector").trigger("myClick"); //觸發(fā)自定義事件
</script>
$("selector").trigger(type[,data])
可以數(shù)組形式傳遞參數(shù)給回調(diào)函數(shù)
P119例:
<script>
$("#btn").bind("myClick",function(event,message1,message2){
$("#test").append("<p>"+message1+message2+"</p>");
});
$("#btn").trigger("myClick", ["我的自定義","事件"]);
</script>
8 其他用法
$("selector").bind("type1 type2",function(){//...})
一次性綁定多個事件類型
P119值得一看的例子
<script>
$(function(){
$("div").bind("mouseover mouseout",function(){
$(this).toggleClass("over"); //切換class
});
})
</script>
$("selector").bind("type.命名空間",function(){//...})
為多個事件添加事件命名空間,便于管理,刪除命名空間后,命名空間下的事件同時刪除,如:
$("div").bind("mouseover.plugin",function(){//...})
$("div").bind("click.plugin",function(){//...})
$("div").unbind(".plugin");
$("selector").trigger("type!")
"!"用來選擇匹配不包含在命名空間中的type方法
1 加載DOM
復(fù)制代碼 代碼如下:
$(document).ready(function(){//...})
DOM加載完畢后執(zhí)行,在可重復(fù)使用上區(qū)別于window.onload=function(){//...}
$(window).load(function(){//...})
window內(nèi)所有對象加載完畢后執(zhí)行,幾等同window.onload=function(){//...}。也可針對selector使用此方法
另:$(document).ready(function(){//...})的簡寫方式:$(function(){//...})或$().ready(function(){//...})
2 事件綁定
復(fù)制代碼 代碼如下:
$("selector").bind()
為元素綁定事件,格式:bind(type[,data],fn),可多次調(diào)用
type事件類型包括:blur, focus, load, resize, scroll, unload, click, dbclick, mousedown, mouseup, mousemove, mouseover, mouseout, mouseenter, mouseleave, change, select, submit, keydown, keypress, keyup, error或者自定義事件
簡寫方法:$("selector").bind(type,function(){//...})等價于$("selector").type(function(){//...})
可傳遞data參數(shù)以供unbind特定事件之用
$("selector").is()
判斷方法
(外:方法多次重用可定義局部變量 var $x = $("selector").方法())
3 合成事件
復(fù)制代碼 代碼如下:
$("selector").hover(enter,leave)
模擬光標(biāo)懸停事件,鼠標(biāo)進入時觸發(fā)enter事件,鼠標(biāo)移出時觸發(fā)leave事件(代替的是bind("mouseenter")和bind("mouseleave"))
使用方法:$("selector").hover(function(){//enter case...},function(){//leave case...})
(外:IE6不支持除a標(biāo)簽外css的:hover偽類的問題——可用此hover事件作為hack來解決)
$("selector").toggle(fn1,fn2,...,fnN)
模擬鼠標(biāo)連續(xù)單擊事件,按照單擊順序按次序循環(huán)執(zhí)行事件
使用方法:$("selector").toggle(function(){//case1...},function(){//case2...},...,function(){//caseN})
特殊用法:切換元素可見狀態(tài),如元素隱藏,單擊toggle觸發(fā)元素可使之可見;元素可見,單擊toggle觸發(fā)元素使之隱藏
P108例:
復(fù)制代碼 代碼如下:
<script>
$(function(){
$("panel h5.head").toggle(function(){
$(this).next().toggle();
},function(){
$(this).next().toggle();
})
})
</script>
4 事件冒泡
$("selector").bind("type",function(event){//event:事件對象...})
event事件對象:只有此函數(shù)內(nèi)部才能訪問到,事件處理函數(shù)執(zhí)行完畢后,事件對象就被銷毀
event.stopPropagation()
在函數(shù)最后用來停止事件冒泡
P111例:
復(fù)制代碼 代碼如下:
<script>
$('span').bind("click",function(event){
var txt = $('msg').html() + "<p>內(nèi)層span元素被單擊</p>";
$('#msg').html(txt);
event.stopPropagation();
})
</script>
event.preventDefault()
阻止元素默認(rèn)行為
例:驗證表單(input為空阻止提交并提示)
復(fù)制代碼 代碼如下:
<script>
$(function(){
$("#submit").bind("click",function(event){
var username=$("#username").val();
if(username==""){
$("#msg").html("<p> 文本框的值不能為空</p>");
event.preventDefault();
}
});
})
</script>
return false;
同時對對象事件停止冒泡和默認(rèn)行為,等價于同時調(diào)用stopPrapagation()和preventDefault()
(外:事件捕獲和事件冒泡是相反的過程,事件捕獲是從DOM頂端往下開始觸發(fā),jQuery不支持,故本書從略)
5 事件對象的屬性
event.type
獲取事件類型
例:
復(fù)制代碼 代碼如下:
<script>
$("a").click(function(event){
alert(event.type);
return false;
})
</script>
上面返回"click"
event.target
獲取觸發(fā)事件的元素
例:
復(fù)制代碼 代碼如下:
<script>
$("a[href=http://baidu.com]").click(function(){
alert(event.target.href);
return false;
})
</script>
上面返回"http://baidu.com"
event.relatedTarget
訪問事件相關(guān)元素
event.pageX / event.pageY
獲取光標(biāo)相對于頁面的x坐標(biāo)和y坐標(biāo)
event.which
在鼠標(biāo)單擊事件中獲取鼠標(biāo)的左、中、右鍵;在鍵盤事件中獲取鍵盤的按鍵(返回值1=鼠標(biāo)左鍵;2=鼠標(biāo)中鍵;3=鼠標(biāo)右鍵)
event.metaKey
鍵盤事件中獲取<ctrl>按鍵
event.originalEvent
指向原始的事件對象
6 移除事件
$("selector").unbind()
移除元素上的事件,格式:$("selector").unbind([type][,data]);(如果沒有參數(shù),則刪除所有綁定的事件;如果提供了事件類型參數(shù),則只刪除該類型的綁定事件;如果把在綁定時傳遞的處理函數(shù)作為第二個參數(shù),則只有這個特定的事件處理函數(shù)會被刪除)
例:
復(fù)制代碼 代碼如下:
<script>
$(function(){
$('#btn').bind("click",myFun1=function(){ //先綁定
$('#test').append("...");
});
})
</script>
<script>
$('#delOne').click(function(){
$('#btn').unbind("click",myFun1); //后刪除
})
</script>
$("selector").one()
綁定一個觸發(fā)一次即被刪除的事件,格式:$("selector").one(type[,data],fn);
7 模擬操作
$("selector").trigger("type");
模擬用戶交互動作,簡寫方法:$("#selector").type(); 格式:$("selector").trigger(type[,data])
例:用單擊替代鼠標(biāo)經(jīng)過
復(fù)制代碼 代碼如下:
<script>
$("selector").mouseover(function{//...});
$("selector2").click(function(){
$("selector").trigger("mouseover"); //或者$("selector").mouseover()
})
</script>
自定義事件的例子
復(fù)制代碼 代碼如下:
<script>
$("selector").bind("myClick",function(){//...}); //綁定自定義事件
$("selector").trigger("myClick"); //觸發(fā)自定義事件
</script>
$("selector").trigger(type[,data])
可以數(shù)組形式傳遞參數(shù)給回調(diào)函數(shù)
P119例:
復(fù)制代碼 代碼如下:
<script>
$("#btn").bind("myClick",function(event,message1,message2){
$("#test").append("<p>"+message1+message2+"</p>");
});
$("#btn").trigger("myClick", ["我的自定義","事件"]);
</script>
8 其他用法
$("selector").bind("type1 type2",function(){//...})
一次性綁定多個事件類型
P119值得一看的例子
復(fù)制代碼 代碼如下:
<script>
$(function(){
$("div").bind("mouseover mouseout",function(){
$(this).toggleClass("over"); //切換class
});
})
</script>
$("selector").bind("type.命名空間",function(){//...})
為多個事件添加事件命名空間,便于管理,刪除命名空間后,命名空間下的事件同時刪除,如:
$("div").bind("mouseover.plugin",function(){//...})
$("div").bind("click.plugin",function(){//...})
$("div").unbind(".plugin");
$("selector").trigger("type!")
"!"用來選擇匹配不包含在命名空間中的type方法
相關(guān)文章
jquery 顯示*天*時*分*秒實現(xiàn)時間計時器
用jquery實現(xiàn)時間計時器,從之前的某個時間段到現(xiàn)在距離多少天多少時多少分多少秒,示例代碼如下,大家拷貝即可使用2014-05-05jQuery ajax serialize()方法的使用以及常見問題解決
使用ajax時,常常需要拼裝input數(shù)據(jù)為'name=abc&sex=1'這種形式,用JQuery的serialize方法可以輕松的完成這個工作接下來介紹jQuery ajax - serialize() 方法定義和用法,感興趣的朋友可以了解下啊,希望本文對你有所幫助2013-01-01jQuery控制DIV層實現(xiàn)由大到小,由遠(yuǎn)及近動畫變化效果
這篇文章主要介紹了jQuery控制DIV層實現(xiàn)由大到小,由遠(yuǎn)及近動畫變化效果,涉及jQuery基于animate方法操作頁面元素實現(xiàn)動畫漸變效果的相關(guān)技巧,需要的朋友可以參考下2015-10-10