一、事件
1 加載DOM
$(document).ready(function(){//...})
DOM加載完畢後執行,在可重複使用上區別於window.οnlοad=function(){//...}
$(window).load(function(){//...})
window內所有對象加載完畢後執行,幾等同window.οnlοad=function(){//...}。也可針對selector使用此方法
另:$(document).ready(function(){//...})的簡寫方式:$(function(){//...}) 或$().ready(function(){//...})
2 事件綁定
$("selector").bind()
爲元素綁定事件,格式:bind(type[,data],fn),可多次調用
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參數以供unbind特定事件之用
$("selector").is()
判斷方法
(外:方法多次重用可定義局部變量 var $x = $("selector").方法())
3 合成事件
$("selector").hover(enter,leave)
模擬光標懸停事件,鼠標進入時觸發enter事件,鼠標移出時觸發leave事件(代替的是bind("mouseenter")和 bind("mouseleave"))
使用方法:$("selector").hover(function(){//enter case...},function(){//leave case...})
(外:IE6不支持除a標籤外css的:hover僞類的問題——可用此hover事件作爲hack來解決)
$("selector").toggle(fn1,fn2,...,fnN)
模擬鼠標連續單擊事件,按照單擊順序按次序循環執行事件
使用方法:$("selector").toggle(function(){//case1...},function() {//case2...},...,function(){//caseN})
特殊用法:切換元素可見狀態,如元素隱藏,單擊toggle觸發元素可使之可見;元素可見,單擊toggle觸發元素使之隱藏
P108例:
<
script
>
$(
function
(){
$(
"
panel h5.head
"
).toggle(
function
(){
$(
this
).next().toggle();
},
function
(){
$(
this
).next().toggle();
})
})
</
script
>
4 事件冒泡
$("selector").bind("type",function(event){//event:事件對象...})
event事件對象:只有此函數內部才能訪問到,事件處理函數執行完畢後,事件對象就被銷燬
event.stopPropagation()
在函數最後用來停止事件冒泡
P111例:
<
script
>
$(
'
span
'
).bind(
"
click
"
,
function
(event){
var
txt
=
$(
'
msg
'
).html()
+
"
<p>內層span元素被單擊</p>
"
;
$(
'
#msg
'
).html(txt);
event.stopPropagation();
})
</
script
>
event.preventDefault()
阻止元素默認行爲
例:驗證表單(input爲空阻止提交併提示)
<
script
>
$(
function
(){
$(
"
#submit
"
).bind(
"
click
"
,
function
(event){
var
username
=
$(
"
#username
"
).val();
if
(username
==
""
){
$(
"
#msg
"
).html(
"
<p> 文本框的值不能爲空</p>
"
);
event.preventDefault();
}
});
})
</
script
>
return false;
同時對對象事件停止冒泡和默認行爲,等價於同時調用stopPrapagation()和preventDefault()
(外:事件捕獲和事件冒泡是相反的過程,事件捕獲是從DOM頂端往下開始觸發,jQuery不支持,故本書從略)
5 事件對象的屬性
event.type
獲取事件類型
例:
<
script
>
$(
"
a
"
).click(
function
(event){
alert(event.type);
return
false
;
})
</
script
>
上面返回"click"
event.target
獲取觸發事件的元素
例:
<
script
>
$(
"
a[href=http://google.com]
"
).click(
function
(){
alert(event.target.href);
return
false
;
})
</
script
>
上面返回"http://google.com"
event.relatedTarget
訪問事件相關元素
event.pageX / event.pageY
獲取光標相對於頁面的x座標和y座標
event.which
在鼠標單擊事件中獲取鼠標的左、中、右鍵;在鍵盤事件中獲取鍵盤的按鍵(返回值1=鼠標左鍵;2=鼠標中鍵;3=鼠標右鍵)
event.metaKey
鍵盤事件中獲取<ctrl>按鍵
event.originalEvent
指向原始的事件對象
6 移除事件
$("selector").unbind()
移除元素上的事件,格式:$("selector").unbind([type][,data]);(如果沒有參數,則刪除所有綁定的事件;如果提供了事件類型參數,則只刪除該類型的綁定事件;如果把在綁定時傳遞的處理函數作爲第二個參數,則只有這個特定的事件處理函數會被刪除)
例:
<
script
>
$(
function
(){
$(
'
#btn
'
).bind(
"
click
"
,myFun1
=
function
(){
//
先綁定
$(
'
#test
'
).append(
"
...
"
);
});
})
</
script
>
<
script
>
$(
'
#delOne
'
).click(
function
(){
$(
'
#btn
'
).unbind(
"
click
"
,myFun1);
//
後刪除
})
</
script
>
$("selector").one()
綁定一個觸發一次即被刪除的事件,格式:$("selector").one(type[,data],fn);
7 模擬操作
$("selector").trigger("type");
模擬用戶交互動作,簡寫方法:$("#selector").type(); 格式:$("selector").trigger(type[,data])
例:用單擊替代鼠標經過
<
script
>
$(
"
selector
"
).mouseover(
function
{
//
...});
$(
"
selector2
"
).click(
function
(){
$(
"
selector
"
).trigger(
"
mouseover
"
);
//
或者$("selector").mouseover()
})
</
script
>
自定義事件的例子
<
script
>
$(
"
selector
"
).bind(
"
myClick
"
,
function
(){
//
...}); //綁定自定義事件
$(
"
selector
"
).trigger(
"
myClick
"
);
//
觸發自定義事件
</
script
>
$("selector").trigger(type[,data])
可以數組形式傳遞參數給回調函數
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方法
本文來自CSDN博客,轉載請標明出處:http://blog.csdn.net/bienfantaisie/archive/2010/04/02/5443405.aspx