给jquery对象添加自定义方法和扩展jquery类

原文地址:http://blog.sina.com.cn/s/blog_944b24ef0101epr5.html

一、给jQuery对象添加自定义方法

      方法一、$.fn. xxx
 
       方法二、jQuery.fn.extend({
             xxx:function(){
                  alert($(this).val());
            }
      });

    方法一示例:

      $.fn.setCursorPosition =  function(position){
                   if( this.lengh ==  0return this;
                   return $( this).setSelection(position, position);
      }
          
      $.fn.setSelection =  function(selectionStart, selectionEnd) {
                   if( this.lengh ==  0return this;
                  input =  this[ 0];
          
                   if (input.createTextRange) {
                               var range = input.createTextRange();
                              range.collapse( true);
                              range.moveEnd( 'character', selectionEnd);
                              range.moveStart( 'character', selectionStart);
                              range. select();
                  }  else if (input.setSelectionRange) {
                              input. focus();
                              input.setSelectionRange(selectionStart, selectionEnd);
                  }
          
                   return this;
      }
          
      $.fn.focusEnd =  function(){
                   this.setCursorPosition( this.val(). length);
      }

    
    方法二示例:

      $.fn.extend({
                  setCursorPosition: function(position){
                               if( this.lengh ==  0return this;
                               return $( this).setSelection(position, position);
                  },
                  setSelection: function(selectionStart, selectionEnd) {
                               if( this.lengh ==  0return this;
                              input =  this[ 0];
                      
                               if (input.createTextRange) {
                                           var range = input.createTextRange();
                                          range.collapse( true);
                                          range.moveEnd( 'character', selectionEnd);
                                          range.moveStart( 'character', selectionStart);
                                          range. select();
                              }  else if (input.setSelectionRange) {
                                          input. focus();
                                          input.setSelectionRange(selectionStart, selectionEnd);
                              }
                      
                               return this;
                  },
                  focusEnd: function(){
                               this.setCursorPosition( this.val(). length);
                  }
      });

   
    以上定义都可如此调用:

      $( '.num'). click( function(){
                  $( '.num').focusEnd();

      });

    
二、扩展jQuery类本身  为类添加新的方法

       方法一、jQuery.extend(object);

       方法二、jQuery. xxx=function(){};
    
    方法一示例:

      $.extend({
            add: function(a,b){ return a+b;},
            a: '2 chi gege '
      });
       alert($.add( 3, 4));  //7
       alert($.a);  //2 chi gege

    方法二示例:

      $.add2 =  function(a,b){
                   return a-b;
      }
       alert($.add2( 4, 3));  //1

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值