各位jQuery达人,请问下面代码我如何在checkLength中获得
$("#test")等处理对象?   function () {
 
       $.fn.extend({
           validate: [],
           aa:[]
       });       $.fn.validate = {
          
           checkLength: function () {
           //怎么在此处获得 $("#test")???
               
           }
       }
   }
   )(jQuery);
   $(document).ready(function () {       var t = $("#test").validate.checkLength();
   });

解决方案 »

  1.   

    this就可以 你这么调用的$("#test").validate.checkLength();
    所以在
     $.fn.validate = {
              
               checkLength: function () {
               //怎么在此处获得 $("#test")???
                   
               }
           }
    里的this就是$("#test")获得的对象
      

  2.   

    $(this) 获得的是extend里面的{ validate: [],aa:[]}对象,而this 获得的是validate对象,都不是哦
      

  3.   

    var t = $("#test").validate.checkLength($("#test"));
    通过传参!
    validate直接在extend里面写完整的话就可以用this了。
      

  4.   

    extend里面也不行哦 $.fn.extend({
                 validate: {
                    checkLength: function () {
                        //$(this)是$.fn              }
                }
            });
      

  5.   

    除非直接写在extend中
    $.fn.extend({
              checkLength: function () {
                        //$(this)是$.fn              
            }
                
            });
    ,但是我想在自定义函数前面加上命名空间validate,不知道怎么搞??
      

  6.   

    除非直接写在extend中$.fn.extend({
              checkLength: function () {
                        //$(this)就是正确的        }
                
            });
    ,但是我想在自定义函数前面加上命名空间validate,不知道怎么搞??
     
      

  7.   

       (function () {
    $.fn.validate = function(methodName){
    validate[methodName].apply(this, Array.prototype.slice.call(arguments, 1));
    }
    var validate = {
    checkLength: function () {
    alert(this.attr('id')); // 此处的this是jquery对象,并非DOM对象,和普通的jquery一致。
    }
    }
       })(jQuery);


       $(document).ready(function () {
           var t = $("#test").validate('checkLength');
           console.dir($("#test"));
       });
      

  8.   

       (function () {
    $.fn.validate = function(methodName){
    validate[methodName].apply(this, Array.prototype.slice.call(arguments, 1));
    }
    var validate = {
    checkLength: function (arg) {
    alert(this.attr('id')); // 此处的this是jquery对象,并非DOM对象,和普通的jquery插件一致。
    alert(arg); // 可以传参数。
    }
    }
       })(jQuery);


       $(document).ready(function () {
           var t = $("#test").validate('checkLength', "AAA");
       });