search
尋找貓咪~QQ 地點 桃園市桃園區 Taoyuan , Taoyuan

[jQuery].on() & .off()

.on()這個方法是將某個selector元素一次全部載入相同的event,.live()、.delegate()和.bind()都是.on()的前身(至於有何差異這邊不多做解釋)

.on()用法

//將selector都載入click事件
$("table td").on("click",function(e){
	alert( $(this).html() );
});

//將selector底下的td元素都載入click事件
$("table").on("click", "td",function(e){
	alert( $(this).html() );
});

//傳值給event
$("table").on("click", {name:"KY"},function(e){
	alert( e.data.name );
});

.off()的功能則是跟.on()相反
.off()用法

//停用selector所有事件
$("table td").off();

//停用selector所有click事件
$("table td").off("click");

/*
關閉selector底下的td元素的click事件
只派的方式需相同才對應的到
例如
$("table td").on("click", test);
要停用事件就必須使用
$("table td").off("click", test);
*/
$("table").on("click", "td", test);
$("table").off("click", "td", test);

function test() {
	alert( $(this).html() );
}
Categories: jQuery



熱門推薦

本文由 blogjohnsonluorg 提供 原文連結

寵物協尋 相信 終究能找到回家的路
寫了7763篇文章,獲得2次喜歡
留言回覆
回覆
精彩推薦