拓展JQuery函数库,开发JQuery 插件

  现在已经是学会$.extend()函数的日后了....
  将新的函数的合并到JQuery函数库,这样就可以用$(selector)._cow_vaginal()的形式调用了
  1.第一种方法(模版),总觉得这种写法不太正统


 (function($){
      $.fn.extend({ 
          _Your_Function_Name:function(){
             //very important property -- arguments
             //此默认参数用来日后获取调用该参数的
             //例如: arguments[0]....arguments[n]
             //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
             //!!!!!Do some thing here!!!!!!!!!!!!
             //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
          };
      });
  })(jQuery);
 



 2.第二种方法


 (function($){
            $.fn._Your_Function_Name= function(options){
            //各种属性、参数
            var defaultsArguments = {
                         argument1:"Niu",
                         argument2:"Bi",    
            }
            //这里获取参数的方式也可以跟上面的一致
            //var options = $.extend(defaults, options);
            this.each(function(){
             //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
             //!!!!!Do some thing here!!!!!!!!!!!!
             //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
            });
        };
        })(jQuery);



 //调用方法就跟JQuery的默认函数一样
 $(selector)._Your_Function_Name();
 $(selector)._Your_Function_Name(
     参数1,
     参数2,
     ...
     参数n
  );


//一个例子
<!DOCTYPE html>
<html>
<head>
    <title>JQuery</title>
    <script src="http://code.jquery.com/jquery-latest.js"></script>
    <style>
        #msg{width:100%;height:100%;}
    </style>
</head>
<body>
    <script>
        (function($){
            
          $.fn.extend({
              _COW_VAGINAL:function(){
                  var arg= arguments[0];
                  var arg2 = arguments[1];
                  var arg3 = arguments[2];
                  alert(arg[1].simple + ","+arg2+","+arg3.arg1);
                  //alert(arg[1]["simple"] + ","+arg2);
              }
          });
           
        })(jQuery);
        
    </script> 
    <script>
        (function($){
            $.fn._COW_VAGINAL_AGAIN = function(options){
            //各种属性、参数
             var arg= arguments[0];
             var arg2 = arguments[1];
            //var options = $.extend(defaults, options);
            this.each(function(){
            //插件实现代码
             alert(arg2);
            });
        };
        })(jQuery);
    </script>
<script>
    var arr = [ "one", "two", "three", "four", "five" ];
    var obj = { one:1, two:2, three:3, four:4, five:5 };
    //alert($.isArray(obj));
    
    $(document).ready(function(){
         $("#msg")._COW_VAGINAL(
                [
                  {name:"name",simple:"标题",required:true},
                  {name:"lls",simple:"标题2",required:true},
                ],
                "let's start",
                {
                    arg1:"NiuBi",
                    arg2:"NiuBi2"
                }
         );
    });

</script>

<div id="msg">

</div>

</body>
</html>

你可能感兴趣的:(JavaScript,function,extend,fn)