$.extend() 或 jQuery.extend() 与 $.fn.Xxx 或 jQuery.fn.extend(object) 之jQuery插件开发

jQuery为开发插件提拱了两个方法

语法现象1:
$.extend() jQuery.extend() 或 jQuery.extend(object)
//可以理解为为jQuery类添加类方法或静态方法
【例子】:

//设计部分
jQuery.extend({ min:
function(a, b) { return a < b ? a : b; }, max: function(a, b) { return a > b ? a : b; } });
//调用部分,还是必须通过jQuery进行引用 jQuery.min(
2,3); // 2 jQuery.max(4,5); // 5
 
语法现象2:
jQuery.fn.extend(object);
因为:jQuery.fn = jQuery.prototype
//可以理解为对jQuery.prototype进得扩展,就是为jQuery类添加“成员函数”。jQuery类的实例可以使用这个“成员函数”。
<head>
<script type="text/javascript">
$.fn.extend({          
    alertWhileClick:function() {            
          $(this).click(function(){                 
                 alert($(this).val());           
           });           
     }       
});
$(function(){
    $("#input1").alertWhileClick(); 
});
</script>
</head>
<body>
<input id="input1" type="text" value="Oman" />
</body>

等价语法

<script type="text/javascript">
$.fn.alertWhileClick = function() {            
            $(this).click(function(){                 
                alert($(this).val());           
            });           
        };
$(function(){
    $("#input1").alertWhileClick();
});
</script>

<body>
<input id="input1" type="text" value="Oman" />
</body>

参考之
《理解jquery的$.extend()、$.fn和$.fn.extend()》 http://caibaojian.com/jquery-extend-and-jquery-fn-extend.html

你可能感兴趣的:($.extend() 或 jQuery.extend() 与 $.fn.Xxx 或 jQuery.fn.extend(object) 之jQuery插件开发)