jquery的绑定事件有几种方式
来源:互联网 发布:淘宝客公司 编辑:程序博客网 时间:2024/06/06 08:33
jquery的绑定事件有几种方式 ,请举例说明其优缺点。
live(),on.delegate()均支持未来新添加元素的事件设置。
b、bind()函数在jquery1.7版本以前比较受推崇,1.7版本出来之后,官方已经不推荐用bind(),
替代函数为on(),这也是1.7版本新添加的函数,同样,可以用来代替live()函数,live()函数在1.9版本已经删除;
c、bind()、.live()或.delegate(),查看源码就会发现,它们实际上调用的都是.on()方法
d、移除.on()绑定的事件用.off()方法。
e、bind()支持Jquery所有版本,live()支持jquery1.9-,delegate()支持jquery1.4.2+,on()支持jquery1.7+
on()函数的使用
多个事件绑定同一个函数
$(document).ready(function(){
$("p").on("mouseover mouseout",function(){
$("p").toggleClass("intro");
});
});
多个事件绑定不同函数
$(document).ready(function(){
$("p").on({
mouseover:function(){$("body").css("background-color","lightgray");},
mouseout:function(){$("body").css("background-color","lightblue");},
click:function(){$("body").css("background-color","yellow");}
});
});
用on()方法绑定多个选择器,多个事件则可以这样写:
$(document).on({
mouseenter: function() {
// Handle mouseenter...
},
mouseleave: function() {
// Handle mouseleave...
},
click: function() {
// Handle click...
}
}, '#header .fixed-feedback-bn, #sb-sec .feedback-bn');
绑定自定义事件
$(document).ready(function(){
$("p").on("myOwnEvent", function(event, showName){
$(this).text(showName + "! What a beautiful name!").show();
});
$("button").click(function(){
$("p").trigger("myOwnEvent",["Anja"]);
});
});
传递数据到函数
function handlerName(event)
{
alert(event.data.msg);
}
$(document).ready(function(){
$("p").on("click", {msg: "You just clicked me!"}, handlerName)
});
适用于未创建的元素
$(document).ready(function(){
$("div").on("click","p",function(){
$(this).slideToggle();
});
$("button").click(function(){
$("<p>This is a new paragraph.</p>").insertAfter("button");
});
});
tip:如果你需要移除on()所绑定的方法,可以使用off()方法处理。
$(document).ready(function(){
$("p").on("click",function(){
$(this).css("background-color","pink");
});
$("button").click(function(){
$("p").off("click");
});
});
在需要为较多的元素绑定事件的时候,优先考虑事件委托,可以带来性能上的好处。将click事件绑定在document对象上,页面上任何元素发生的click事件都冒泡到document对象上得到处理。
- jquery的绑定事件有几种方式
- jquery解除hover事件的绑定方式
- jQuery绑定事件的四种方式
- jQuery绑定事件的四种方式
- jQuery绑定事件的四种方式
- jquery的几种事件绑定方式
- jQuery绑定事件的四种方式
- jQuery的绑定事件
- jQuery的事件绑定
- jquery的绑定事件
- jquery的事件绑定
- JQuery事件的绑定
- jQuery的事件绑定
- jquery中取消和绑定hover事件的正确方式
- jquery中取消和绑定hover事件的正确方式
- jquery几种事件绑定方式的比较
- jQuery为动态元素绑定事件的方式
- jQuery绑定事件的四种方式介绍
- new Handler()和new Handler(Looper.getMainLooper())的使用区别
- Go学习笔记(四)
- 一款极度偷懒的假分页js代码(只适用于要求很低的页面)
- MySQL函数一览_MySQL函数全部汇总
- TensorFlow学习:MNIST
- jquery的绑定事件有几种方式
- Spring 学习笔记(5)—— 简化xml配置文件的配置方式
- Wannafly挑战赛2 B 题 Travel 【最短路 + 思维】
- python3及wxpythonGUI多线程防假死与线程消息传递消息(最新)
- J2EE学习笔记
- List、Map、Set三个接口,存取元素的特点
- 洛谷 P2564 [SCOI2009]生日礼物
- [ [资源贴] ]数据集、函数、python、tensorflow、caffe等相关的资源分享
- 交叉编译遇到一些问题