jquery中事件的绑定有2种
1:eventName(fn);编码效率略高,部分js事件没有实现,所以不能添加
2:on(eventName,fn):编码效率较低/所有js事件都可以添加
fn:代表回调函数,eventname:代表事件名字
他们和原生js同的是,他们可以外某个元素添加一个或者多个不同的事件,且不会覆盖
// */
$("button").click(function(){
// alert("讨厌你");
console.log("ajkb ");
});
$("button").click(function(){
// alert("讨厌hkb 你");
console.log("jlkgbsg");
});
$("button").mouseleave(function(){
alert("太阿星你 mouseleave");
});
$("button").mouseenter(function(){
alert("太阿星你 mouseenter");
});
$("button").on("click",function(){
alert("讨厌你");
});
$("button").on("click",function(){
alert("讨很久");
});
$("button").on("mouseenter",function(){
alert("mouseenter");
});
$("button").on("mouseleave",function(){
alert("mouseleave");
});
我们可以off()方法对元素上的事件进行移出
元素.off()
off()方法里面可以不传参,如果不传代表移出所有事件
off()方法里面如果传2个参数,会移除指定类型的事件 代码如下:
function test1(){
// alert("讨厌你");
alert("讨厌你");
}
function test2(){
alert("西黄你");
// console.log("西黄你");
}
//编写jq的相关代码
$("button").click(test1);
$("button").click(test2);
// $("button").click(function(){
// alert("讨厌你");
// });
// $("button").click(function(){
// alert("讨厌你");
// });
$("button").mouseleave(function(){
alert("太阿星你 mouseleave");
});
$("button").mouseenter(function(){
alert("太阿星你 mouseenter");
});
//off()方法如果不传参就移除所以所有事件
$("button").off();
//off如果传递1个参数,会移除所有指定类型的事件
//$("button").off("click");
//如果传递2个参数,会移除指定类型的指定事件
// $("button").off("click",test1);