标签:
1. 事件绑定与解绑 2. 事件冒泡与阻止 3. 自定义事件
1. $(document).ready(function(){
// $("button").click(function(){
// $("#pid").text("hello world");
// });
$("#btn").bind("click",clickHandler1);
$("#btn").bind("click",clickHandler2);
$("#btn").unbind("click");
$("#btn").unbind("click",clickHandler1);
$("#btn").on("click",clickHandler1);
$("#btn").off("click");
});
function clickHandler1(e){
console.log("clickHandler1");
}
function clickHandler2(e){
console.log("clickHandler2");
}
// If I select the ( let it focus
// event: click dblclick mouseenter mouseleave
2.
$(document).ready(function(){
$("body").bind("click",bodyHandler);
$("div").bind("click",divHandler1);
$("div").bind("click",divHandler2);
});
function bodyHandler(event){
console.log(event);
}
function divHandler1(event){
conlog(event);
// event.stopPropagation(); // stop parent event.
event.stopImmediatePropagation(); // stop all others.
}
function divHandler2(event){
console.log(event);
}
function conlog(event){
console.log(event);
}
3.
$(document).ready(function(){
$("#btn").click(function(){
var e = jQuery.Event("MyEvent");
$("#btn").trigger(e);
});
$("#btn").bind("MyEvent",function(event){
console.log(event);
})
});
// same code:
var btn;
$(document).ready(function(){
btn = $("#btn");
btn.click(function(){
var e = jQuery.Event("MyEvent");
btn.trigger(e);
});
$("#btn").bind("MyEvent",function(event){
console.log(event);
})
});
标签:
原文地址:http://www.cnblogs.com/htmlphp/p/4838234.html