jquery插件机制

一,jquery.fn.extend(object)

扩展 jQuery 元素集来提供新的方法(一般用来制做插件),须要指定dom对象调用jquery

示例:dom

给input对象增长两个插件方法,jquery.fn.extend代码单独做为通用的插件文件common/check.js,在须要用此功能的地方引入$Import("common.check");函数

jQuery插件代码:this

(function($) {
    jQuery.fn.extend({
      check: function() {
        return this.each(function() { this.checked = true; });
      },
      uncheck: function() {
        return this.each(function() { this.checked = false; });
      }
    });
})(jQuery);

使用:spa

$Import("common.check");
$(function () {
    $("input[type=checkbox]").check();
    $("input[type=radio]").uncheck();
});

二,jquery.extend(object)

做用:扩展jQuery对象自己,用来在jQuery命名空间上增长新函数。 插件

示例:给input对象增长两个插件方法,jquery.extend代码单独做为通用的插件文件common/compare.js,在须要用此功能的地方引入$Import("common.compare");code

jQuery插件代码:对象

(function($) {
    jQuery.extend({
      min: function(a, b) { return a < b ? a : b; },
      max: function(a, b) { return a > b ? a : b; }
    });
})(jQuery);

使用:input

$Import("common.compare");
$(function(){
    jQuery.min(2,3); // => 2
    jQuery.max(4,5); // => 5
});

以上均是做为插件使用,也能够直接在一个js文件中引用io

相关文章
相关标签/搜索