jQuery--事件绑定(bind()、one()、toggle())详解

逃离我推掉我的手 2021-08-13 14:22 559阅读 0赞

事件绑定

所谓事件绑定,是指将页面元素的事件类型与事件处理函数关联到一起,当事件触发时调用事件绑定的事件处理函数。在jQuery中,提供了强大的API来执行事件的绑定操作,如bing()、one()、toggle()、live()、delegate()、on()和hover()等。

1. bind()方法:
bind()方法用于对匹配元素的特定事件绑定的事件处理函数,语法格式如下:

  1. bing(types, [data],fn)
  • type:表示事件类型,是一个或多个事件类型构成的字符串,类型之间由空格隔开,事件类型包括鼠标事件或键盘事件,鼠标时间包括click、submit、mouseover和mouseup等,键盘事件包括keydown和keyup等
  • data(可选):表示传递给函数的额外数据,在事件处理函数中通过event.data来获得传入的额外数据
  • fn:表示所绑定的事件按处理函数

示例:

  1. //绑定事件
  2. $("#bindBtn").bind("click", function(){
  3. $("#rightDiv").text("使用bind()方法绑定事件处理");
  4. });
  5. //为一个对象同时绑定mouseenter和mouseleave事件
  6. $("#bindBtn2").bind("mouseenter mouseleave",function(){
  7. $(this).toggleClass("entered");
  8. });
  9. //为一个对象同时绑定多个事件,且每个事件具有单独的处理函数
  10. $("#manyBindBtn").bind({
  11. click:function(){ $("#rightDiv").slideToggle();},
  12. mouseover:function(){ $("#rightDiv").css("background-color","red");},
  13. mouseout:function(){ $("#rightDiv").css("background-color","yellow");}
  14. });

jQuery为常用的事件(如click、mouseover和mouseout等)提供了一种简写方式,与bind()方法实现的效果完全相同

  1. $("#shortBindBtn").click(function(){
  2. $("#rightDiv").text("事件绑定缩写方式实现");
  3. });

2. one()方法:
one()方法用于对匹配元素的特定事件绑定一个一次性的事件处理函数,事件处理函数只会被执行一次。语法格式如下:

  1. one(types,[data],fn)
  • types:表示事件类型,是一个或多个事件类型构成的字符串,类型之间由空格隔开
  • data(可选):表示传递给函数的额外数据,在事件处理函数中通过event.data来获得传入的额外数据
  • fn:表示所绑定的事件按处理函数

示例:

  1. $("#rightDiv").one("click", function(){
  2. alert($(this).text());
  3. });

3. toggle()方法(1.9版本之前):
toggle()方法用于模拟鼠标连续单击事件,语法格式如下:

  1. toggle(([speed],[easing],[fn1,fn2,fn3,...,fnN]))
  • speed:用于设置元素的隐藏(或显示速度),默认是0ms,取值范围是slow、normal、fast或毫秒数
  • easing:用于指定动画的切换效果,取值是swing(默认)和linear
  • fn1,fn2,fn3,…,fnN:表示1~n个事件处理函数。fn1表示第一次单击时的处理函数,fn2表示第二次单击时的处理函数等等
  • 同时具有参数speed、fn时,表示以speed速度显示或隐藏,在动画完成后执行fn事件处理函数

示例:

  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>jQuery基本操作事件绑定</title>
  6. <script type="text/javascript" src="js/jquery-1.x.js"> </script>
  7. <script type="text/javascript" src="js/jquery-migrate-1.2.1.js"> </script>
  8. <style type="text/css"> div{ width:200px;height:200px;border:1px solid #666;} #leftDiv{ float:left; margin:0 auto;} #rightDiv{ float:right;} .entered{ background-color:#66F;} </style>
  9. </head>
  10. <body>
  11. <div id="leftDiv">
  12. <input type="button" value="bind事件绑定" id="bindBtn"/>
  13. <input type="button" value="一次绑定两个事件" id="bindBtn2"/>
  14. <input type="button" value="多事件绑定" id="manyBindBtn"/>
  15. <input type="button" value="事件绑定缩写方式" id="shortBindBtn"/>
  16. <input type="button" value="toggle()多形式事件" id="toggleBtn"/>
  17. <input type="button" value="toggle()动画事件" id="toggleAnimateBtn"/>
  18. </div>
  19. <div id="rightDiv">右侧展示区</div>
  20. <script type="text/javascript"> $(function(){ //绑定事件 $("#bindBtn").bind("click", function(){ $("#rightDiv").text("使用bind()方法绑定事件处理"); }); //为一个对象同时绑定mouseenter和mouseleave事件 $("#bindBtn2").bind("mouseenter mouseleave",function(){ $(this).toggleClass("entered"); }); //为一个对象同时绑定多个事件,且每个事件具有单独的处理函数 $("#manyBindBtn").bind({ click:function(){ $("#rightDiv").slideToggle();}, mouseover:function(){ $("#rightDiv").css("background-color","red");}, mouseout:function(){ $("#rightDiv").css("background-color","yellow");} }); //事件绑定的缩写形式 $("#shortBindBtn").click(function(){ $("#rightDiv").text("事件绑定缩写方式实现"); }); //one()方式绑定一次性事件 $("#rightDiv").one("click", function(){ alert($(this).text()); }); //模拟连续多次单击事件 $("#toggleBtn").toggle(function(){ $(this).css("background-color","red"); },function(){ $(this).css("background-color","green"); },function(){ $(this).css("background-color","yellow"); },function(){ $(this).css("background-color","white"); } ); //动画效果结束后,再事件调用处理函数 $("#toggleAnimateBtn").click(function(){ //$("#newsContent").toggle(10000); $("#rightDiv").toggle("slow","swing",function(){ $("#toggleAnimateBtn").css("background-color","red"); }); }); }); </script>
  21. </body>
  22. </html>

在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,559人围观)

还没有评论,来说两句吧...

相关阅读

    相关 jQuery动态事件

    jquery中绑定事件一般使用bind,或者click,但是这只能是对已经加载好的元素定义事件,那些后来添加插入的元素则需要另行绑定。在1.7版本以前使用live。但是在1.8

    相关 jQuery--解除事件

    解除事件绑定 在元素绑定事件之后,当在某个时刻不再需要该事件处理时,可以解除所绑定的事件。在jQuery中提供了unbind()和undelegate()方法,分别用于解