码迷,mamicode.com
首页 > Web开发 > 详细

jquery选择器和事件

时间:2015-09-25 16:05:09      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:

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);
})
});

 

jquery选择器和事件

标签:

原文地址:http://www.cnblogs.com/htmlphp/p/4838234.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!