首页  >  工具  > $.sub()

返回值:jQueryjQuery.sub()

V1.5jQuery $.sub() 方法概述

可创建一个新的jQuery副本,不影响原有的jQuery对像。

有两个具体使用jQuery.sub()创建案例。首先是提供完全没有破坏jQuery原有一切的方法,另一个用于帮助做jQuery插件封装和基本命名空间。

请注意,jQuery.sub()不会做任何特殊的隔离 - 这不是它的意图。所有关于jQuery的sub'd版本的方法将仍然指向原来的jQuery。(绑定和触发仍将通过主jQuery的事件,数据将通过主绑定的元素的jQuery,Ajax的查询和活动将通过主jQuery的运行,等等)。

请注意,如果你正在寻找使用这个开发插件,应首先认真考虑使用一些类似jQuery UI widget工厂,这两个状态和插件管理子方法。 使用jQuery UI widget的一些例子建立一个插件。

这种方法的具体使用情况下可以通过一些例子最好的描述。

该方法是在jQuery 1.5中引入的,但是被证明不是很有用,将被移到jQuery 1.9兼容性插件中。

示例

描述:

添加一个jQuery的方法,以便它不会受到外部分:

jQuery 代码:
(function(){
    var sub$ = jQuery.sub();

    sub$.fn.myCustomMethod = function(){
      return 'just for me';
    };

    sub$(document).ready(function() {
      sub$('body').myCustomMethod() // 'just for me'
    });
  })();
  
  typeof jQuery('body').myCustomMethod // undefined

描述:

改写一些jQuery的方法,以提供新的功能。

jQuery 代码:
(function() {
  var myjQuery = jQuery.sub();

  myjQuery.fn.remove = function() {
    // New functionality: Trigger a remove event
    this.trigger("remove");

    // Be sure to call the original jQuery remove method
    return jQuery.fn.remove.apply( this, arguments );
  };

  myjQuery(function($) {
    $(".menu").click(function() {
      $(this).find(".submenu").remove();
    });

    // A new remove event is now triggered from this copy of jQuery
    $(document).bind("remove", function(e) {
      $(e.target).parent().hide();
    });
  });
})();

// Regular jQuery doesn't trigger a remove event when removing an element
// This functionality is only contained within the modified 'myjQuery'.

描述:

创建一个插件,它返回插件的具体办法。

jQuery 代码:
(function() {
  // Create a new copy of jQuery using sub()
  var plugin = jQuery.sub();

  // Extend that copy with the new plugin methods
  plugin.fn.extend({
    open: function() {
      return this.show();
    },
    close: function() {
      return this.hide();
    }
  });

  // Add our plugin to the original jQuery
  jQuery.fn.myplugin = function() {
    this.addClass("plugin");

    // Make sure our plugin returns our special plugin version of jQuery
    return plugin( this );
  };
})();

$(document).ready(function() {
  // Call the plugin, open method now exists
  $('#main').myplugin().open();

  // Note: Calling just $("#main").open() won't work as open doesn't exist!
});