jQuery on()方法是官方推荐的绑定事件的一个方法。
$(selector).on(event,childSelector,data,function,map)
由此扩展开来的几个以前常见的方法有.
bind()
$("p").bind("click",function(){ alert("The paragraph was clicked."); }); $("p").on("click",function(){ alert("The paragraph was clicked."); });
delegate()
$("#div1").on("click","p",function(){ $(this).css("background-color","pink"); }); $("#div2").delegate("p","click",function(){ $(this).css("background-color","pink"); });
live()
$("#div1").on("click",function(){ $(this).css("background-color","pink"); }); $("#div2").live("click",function(){ $(this).css("background-color","pink"); });
以上三种方法在jQuery1.8之后都不推荐使用,官方在1.9时已经取消使用live()方法了,所以建议都使用on()方法。
tip:如果你需要移除on()所绑定的方法,可以使用off()方法处理。
$(document).ready(function(){ $("p").on("click",function(){ $(this).css("background-color","pink"); }); $("button").click(function(){ $("p").off("click"); }); });
tip:如果你的事件只需要一次的操作,可以使用one()这个方法
$(document).ready(function(){ $("p").one("click",function(){ $(this).animate({fontSize:"+=6px"}); }); });
trigger()绑定
$(selector).trigger(event,eventObj,param1,param2,...) $(document).ready(function(){ $("input").select(function(){ $("input").after(" Text marked!"); }); $("button").click(function(){ $("input").trigger("select"); }); });
多个事件绑定同一个函数
$(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");} }); });
绑定自定义事件
$(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"); }); });
jQuery绑定事件的方法有几种,推荐使用.on()方法绑定,原因有两点:
1.on()方法可以绑定动态添加到页面元素的事件
比如动态添加到页面的DOM元素,用.on()方法绑定的事件不需要关心注册该事件的元素何时被添加进来,也不需要重复绑定。有的同学可能习惯于用.bind()、.live()或.delegate(),查看源码就会发现,它们实际上调用的都是.on()方法,并且.live()方法在jQuery1.9版本已经被移除。
bind: function( types, data, fn ) { return this.on( types, null, data, fn ); }, live: function( types, data, fn ) { jQuery( this.context ).on( types, this.selector, data, fn ); return this; }, delegate: function( selector, types, data, fn ) { return this.on( types, selector, data, fn ); }
移除.on()绑定的事件用.off()方法。
2.on()方法绑定事件可以提升效率
很多文章都提到了利用事件冒泡和代理来提升事件绑定的效率,大多都没列出具体的差别,所以为了求证,我做一个小测试。
假设页面添加了5000个li,用chrome开发者工具Profiles测试页面载入时间。
普通绑定(姑且这么称呼它)
$('li').click(function(){ console.log(this) });
绑定过程的执行时间
2013-08-13_190358
普通绑定相当于在5000li上面分别注册click事件,内存占用约4.2M,绑定时间约为72ms。
.on()绑定
$(document).on('click', 'li', function(){ console.log(this) })
绑定过程的执行时间
2013-08-13_191010
.on()绑定利用事件代理,只在document上注册了一个click事件,内存占用约2.2M,绑定时间约为1ms。
以上就是本文的全部内容,希望对大家学习jquery on()方法有所帮助。
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
- 群星.1995-烧得厉害VOL.4两个世界【滚石】【WAV+CUE】
- 归龙潮中浦城区宝箱位置收集大全 中浦区宝箱解密攻略
- 黑神话悟空上品幽灯鬼精魄获取方法一览|上品幽灯鬼精魄收集攻略
- 《电锯糖心:RePOP》发售首日表现亮眼,玩家峰值超越《星鸣特攻》
- NewViennaOctet,ViennaWindSoloists-TheDeccaRecordings(2024)18CD[FLAC]-8
- 金梅子-《靓声妹3又见炊烟》[WAV+CUE]
- 杨阳《约定HQCDII》头版限量编号2024[WAV+CUE]
- 曝《黑神话:悟空》DLC明年推出 农历新年前后
- PS5日本涨价19% 索尼股价应声上涨2.7%
- 《黑神话》发行平台遭大规模网攻 外媒却鲜有报道
- 伍佰《爱你伍佰年 世纪典藏原音精选3CD》[WAV+CUE][2.3GB]
- 伍佰《爱情的尽头》[WAV+CUE][2.6GB]
- 伍佰 《光环摇滚之声DSD》[WAV+CUE][500MB]
- 王菲.1999-只爱陌生人【EMI百代】【WAV+CUE】
- 林美音.1993-酒后的心声【瑞华】【WAV+CUE】