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

相關文章
相關標籤/搜索