[ jQuery] jquery的click(),bind(),on()区别~

来源:互联网 发布:淘宝卖家怎么修改物流 编辑:程序博客网 时间:2024/05/17 23:12

这里只是突然发现jquery 1.7 之后的事件绑定推荐了一些变化。jquery推荐使用on方法来绑定事件。

相信以前都是用的click,bind,unbind方法吧。

如果你仔细看看jquery的源码的话,那么你会发现bind的方法也是用的on方法来实现的。

在一定程度上on方法比bind或者是click之类的方法,更高效。

下面只是一个demo,当然这里包含了jquery的插件的写法。

关于jquery的插件的写法,可以自行百度。如果你的自学能力够厉害的话,可以查看jquery的源码。

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">  
  3. <head>  
  4.   <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />  
  5.    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>  
  6.   <title></title>  
  7. </head>  
  8. <body>  
  9.     <input type="button" value="test" id="test"/>  
  10.     <script type="text/javascript">  
  11.         $(function(){  
  12.             $("#test").hello({  
  13.                 "size":1  
  14.             });  
  15.         });  
  16.     </script>  
  17.     <script type="text/javascript">  
  18.     ;(function($){  
  19.         $.fn.hello = function(options){  
  20.             var defaults = {"size":0},  
  21.   
  22.                 opts     = $.extend({},defaults,options),  
  23.   
  24.                 show = {  
  25.                     play:function(options){  
  26.                         var _root =  this;   
  27.                         _root.autoPlay();  
  28.                         _root.eventClick();  
  29.                     },  
  30.                     autoPlay:function(){  
  31.                         console.log("auto");  
  32.                     },  
  33.                     eventClick:function(){  
  34.                         //$("#test").on("click",{show:"dd"},function(e){  
  35.                         //    console.log("click :" +  e.data.show);  
  36.                         //});  
  37.   
  38.                         $("#test").on({  
  39.                             click:function(){  
  40.                                 alert("click");  
  41.                             },  
  42.                             mouseenter:function(){  
  43.                                 alert("enter");  
  44.                             },  
  45.                             mouseleave:function(){  
  46.                                 alert("leave");  
  47.                             }  
  48.                         });  
  49.                     }  
  50.                 };  
  51.             return this.each(function(){  
  52.                 show.play(opts);  
  53.             });  
  54.         };  
  55.     })(jQuery);  
  56.     </script>  
  57. </body>  
  58. </html>  

转载自 http://blog.csdn.net/yangzhihello/article/details/17632187

0 0