$(function(){ //在jq这样的双重绑定事件不会重叠 $("button").click(function(){ alert(1) }) $("button").click(function(){ alert(2) }) })
绑定方法2 事件源 . bind(" 事件1 事件二",function(){}) 能够绑定多个事件jquery
//绑定事件 方法2 bind 能够给一个对象写多个触发条件,执行一个函数 $("button").bind("click mouseenter",function(){ alert(1) })
绑定方法3 事件源的父亲 . delegate(" 事件源" ," 事件1 事件2",function(){})json
//第三种绑定方式 能够给他父级的子盒子绑定事件 $(".box").delegate(".box2","click mouseenter",function(){ alert(2) })
$(".box").on("click",".box2",{"name":111},function(event){ console.log(event.data) })
//最经常使用的事件绑定 $(".box2").on("click",{},function(){ alert(1) }) }
解绑事件浏览器
//第一中解绑事件 针对 click mouseenter bind绑定的事件 用什么绑定的就用什么解除 $("button").unbind("click");
//第二种解绑方式 delegate $(".box").delegate(".box2","click mouseenter",fn); $(".box").delegate(".box2","click mouseenter",fn2); function fn(){ alert(2) } function fn2(){ alert(3) } //这样是解除了fn事件的因此事件 $(".box").undelegate(".box2","mouseenter",fn);
//第三种解绑的方法 $(".box").on("click mouseenter",".box2",{"name":111},function(event){ alert("0") }) $(".box").off("click",".box2")
事件被触发函数
$(function () { $(document).on("click mouseenter", function () { alert("页面被点击了"); }); //事件触发(1)(触发浏览器行为) $(document).click(); $(document).mouseenter(); //事件触发(2)(触发浏览器行为) 点击document的地方都会出发事件 $(document).trigger("mouseenter"); $(document).trigger("click"); //事件触发(3)(不触发浏览器行为) 点击document 地方不出发事件 $(document).triggerHandler("mouseenter"); $(document).triggerHandler("click"); $("input").on("focus", function () { alert("我获取了焦点!"); }); //事件触发(2)(触发浏览器行为)(执行程序,触动事件) $(document).click(function () { $("input").trigger("focus"); }); // //事件触发(3)(不触发浏览器行为)(只执行程序 " console.log("我获取了焦点!");",不触动事件(获取鼠标) $(document).click(function () { $("input").triggerHandler("focus"); }); }) </script>
<script> $(function(){ $(document).on("click",function(e){ console.log(e.data); console.log(e.currentTarget); console.log(e.pageX); console.log(e.target); console.log(e.stopPropagation); console.log(e.preventDefault); console.log(e.type); console.log(e.which); console.log(e.keyCode); }) }) </script>
$(function(){
//在jq这样的双重绑定事件不会重叠
$("button").click(function(){
alert(1)
})
$("button").click(function(){
alert(2)
})
})
//绑定事件 方法2 bind 能够给一个对象写多个触发条件,执行一个函数
$("button").bind("click mouseenter",function(){
alert(1)
})
//第三种绑定方式 能够给他父级的子盒子绑定事件
$(".box").delegate(".box2","click mouseenter",function(){
alert(2)
})
$(".box").on("click",".box2",{"name":111},function(event){
console.log(event.data)
})
//最经常使用的事件绑定
$(".box2").on("click",{},function(){
alert(1)
})
}
//第一中解绑事件 针对 click mouseenter bind绑定的事件 用什么绑定的就用什么解除
$("button").unbind("click");
//第二种解绑方式 delegate
$(".box").delegate(".box2","click mouseenter",fn);
$(".box").delegate(".box2","click mouseenter",fn2);
functionfn(){
alert(2)
}
functionfn2(){
alert(3)
}
//这样是解除了fn事件的因此事件
$(".box").undelegate(".box2","mouseenter",fn);
//第三种解绑的方法
$(".box").on("click mouseenter",".box2",{"name":111},function(event){
alert("0")
})
$(".box").off("click",".box2")
$(function () {
$(document).on("click mouseenter", function () {
alert("页面被点击了");
});
//事件触发(1)(触发浏览器行为)
$(document).click();
$(document).mouseenter();
//事件触发(2)(触发浏览器行为) 点击document的地方都会出发事件
$(document).trigger("mouseenter");
$(document).trigger("click");
//事件触发(3)(不触发浏览器行为) 点击document 地方不出发事件
$(document).triggerHandler("mouseenter");
$(document).triggerHandler("click");
$("input").on("focus", function () {
alert("我获取了焦点!");
});
//事件触发(2)(触发浏览器行为)(执行程序,触动事件)
$(document).click(function () {
$("input").trigger("focus");
});
// //事件触发(3)(不触发浏览器行为)(只执行程序 " console.log("我获取了焦点!");",不触动事件(获取鼠标)
$(document).click(function () {
$("input").triggerHandler("focus");
});
})
</script>
<script>
$(function(){
$(document).on("click",function(e){
console.log(e.data);
console.log(e.currentTarget);
console.log(e.pageX);
console.log(e.target);
console.log(e.stopPropagation);
console.log(e.preventDefault);
console.log(e.type);
console.log(e.which);
console.log(e.keyCode);
})
})
</script>