首页 > 其他分享 >jQuery on()方法示例及jquery on()方法的优点

jQuery on()方法示例及jquery on()方法的优点

时间:2022-08-20 17:13:51浏览次数:96  
标签:jQuery jquery 示例 function 绑定 background document 方法 click

https://www.jb51.net/article/71614.htm#

jQuery on()方法是官方推荐的绑定事件的一个方法。

1 $(selector).on(event,childSelector,data,function,map)

由此扩展开来的几个以前常见的方法有.

bind()

1 2 3 4 5 6 $("p").bind("click",function(){    alert("The paragraph was clicked.");  });  $("p").on("click",function(){    alert("The paragraph was clicked.");  });

delegate()

1 2 3 4 5 6 $("#div1").on("click","p",function(){     $(this).css("background-color","pink");   });   $("#div2").delegate("p","click",function(){     $(this).css("background-color","pink");   });

live()

1 2 3 4 5 6 $("#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()方法处理。

1 2 3 4 5 6 7 8 $(document).ready(function(){   $("p").on("click",function(){     $(this).css("background-color","pink");   });   $("button").click(function(){     $("p").off("click");   }); });

tip:如果你的事件只需要一次的操作,可以使用one()这个方法

1 2 3 4 5 $(document).ready(function(){   $("p").one("click",function(){     $(this).animate({fontSize:"+=6px"});   }); });

trigger()绑定

1 2 3 4 5 6 7 8 9 $(selector).trigger(event,eventObj,param1,param2,...) $(document).ready(function(){   $("input").select(function(){     $("input").after(" Text marked!");   });   $("button").click(function(){     $("input").trigger("select");   }); });

多个事件绑定同一个函数

1 2 3 4 5 $(document).ready(function(){  $("p").on("mouseover mouseout",function(){   $("p").toggleClass("intro");  }); });

多个事件绑定不同函数

1 2 3 4 5 6 7 $(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");}  }); });

绑定自定义事件

1 2 3 4 5 6 7 8 $(document).ready(function(){  $("p").on("myOwnEvent", function(event, showName){   $(this).text(showName + "! What a beautiful name!").show();  });  $("button").click(function(){   $("p").trigger("myOwnEvent",["Anja"]);  }); });

传递数据到函数

1 2 3 4 5 6 7 function handlerName(event) {  alert(event.data.msg); } $(document).ready(function(){  $("p").on("click", {msg: "You just clicked me!"}, handlerName) });

适用于未创建的元素

1 2 3 4 5 6 7 8 $(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版本已经被移除。

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 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测试页面载入时间。

普通绑定(姑且这么称呼它)

1 2 3 $('li').click(function(){   console.log(this) });

绑定过程的执行时间

2013-08-13_190358

普通绑定相当于在5000li上面分别注册click事件,内存占用约4.2M,绑定时间约为72ms。

.on()绑定

1 2 3 4 5 $(document).on('click', 'li', function(){   console.log(this) })

绑定过程的执行时间

2013-08-13_191010

.on()绑定利用事件代理,只在document上注册了一个click事件,内存占用约2.2M,绑定时间约为1ms。

以上就是本文的全部内容,希望对大家学习jquery on()方法有所帮助。

标签:jQuery,jquery,示例,function,绑定,background,document,方法,click
From: https://www.cnblogs.com/jmbt/p/16608165.html

相关文章

  • JQuery事件绑定
    JQuery事件绑定1.jquery标准的绑定方式jq对象.事件方法(回调函数)HTML代码<!DOCTYPEhtml><html><head><metacharset="utf-8"><title>绑......
  • Java SE 10 Application Class-Data Sharing 示例
    JavaSE10ApplicationClass-DataSharing示例作者:Grey原文地址:JavaSE10ApplicationClass-DataSharing示例Class-DataSharingCDS全称Class-DataSharing。......
  • JQuery_DOM操作_属性操作_CRUD操作
    CRUD操作append():父元素将子元素追加到末尾对象1.append(对象2):将对象2添加到对象1元素内部,并且在末尾prepend():父元素将子元素追加到开头对象1.prepend(对象2):将......
  • JQuery遍历
    JQuery遍历1.js的遍历方式for(初始化值;循环结束条件;步长)HTML代码<!DOCTYPEhtml><html><head><metacharset="UTF-8"><title></title><script......
  • AJAX实现JQuery
    AJAX实现JQuery实现方式ajax()语法$.ajax(url,[settings])记着导入jquery<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><title>1-原生js......
  • JQuery案例和插件机制
    案例—广告显示和隐藏需求:1、当页面加载完,3秒后。自动显示广告2、广告显示5秒后,自动消失分析:1、使用定时器来完成,setTimeout(执行一次定时器)2、分析......
  • Jquery插件机制
    插件:增强JQuery的功能实现方式:$.fn.extend(object)增强通过JQuery获取的对象的功能$("#id")$.extend(object)增强JQuery对象自身的功能$/JQuery<!DOCTY......
  • JQuery案例_广告显示和隐藏&JQuery案例_抽奖
    JQuery案例_广告显示和隐藏需求:当页面加载完,3秒后自动显示广告广告显示5秒后,自动消失分析:使用定时器来完成。setTimeout(执行一次定时器)分析发现JQuery的显示......
  • JQuery_案例
    JQuery_案例隔行换色HTML代码<!DOCTYPEhtml><html><head><metacharset="utf-8"><title></title><scriptsrc="../js/jquery-3.3.1......
  • JQuery插件机制
    JQuery插件机制插件:增强JQuery的功能 1.实现方式: 1.$.fn.extend(object)  (对象) 增强通过JQuery获取的对象的功能 $("#id") 2......