- jQuery.foo = function() {
- alert('This is a test. This is only a test.');
- };
- jQuery.foo = function() {
- alert('This is a test. This is only a test.');
- };
- jQuery.bar = function(param) {
- alert('This function takes a parameter, which is "' + param + '".');
- };
- 调用时和一个函数的一样的:jQuery.foo();jQuery.bar();或者$.foo();$.bar('bar');
1.3使用jQuery.extend(object);
- jQuery.extend({
- foo: function() {
- alert('This is a test. This is only a test.');
- },
- bar: function(param) {
- alert('This function takes a parameter, which is "' + param +'".');
- }
- });
- jQuery.myPlugin = {
- foo:function() {
- alert('This is a test. This is only a test.');
- },
- bar:function(param) {
- alert('This function takes a parameter, which is "' + param + '".');
- }
- };
- 采用命名空间的函数仍然是全局函数,调用时采用的方法:
- $.myPlugin.foo();
- $.myPlugin.bar('baz');
- (function($){
- $.fn.extend({
- pluginName:function(opt,callback){
- // Our plugin implementation code goes here.
- }
- })
- })(jQuery);
形式2:
- (function($) {
- $.fn.pluginName = function() {
- // Our plugin implementation code goes here.
- };
- })(jQuery);
上面定义了一个jQuery函数,形参是$,函数定义完成之后,把jQuery这个实参传递进去.立即调用执行。这样的好处是,我们在写jQuery插件时,也可以使用$这个别名,而不会与prototype引起冲突.
2.1 在JQuery名称空间下申明一个名字
- $.fn.hilight = function() {
- // Our plugin implementation code goes here.
- };
- 我们的插件通过这样被调用:
- $('#myDiv').hilight();
$.fn.hilight = function() { // Our plugin implementation code goes here. }; 我们的插件通过这样被调用: $('#myDiv').hilight();
2.2 接受 options 参数以控制插件的行为
- // plugin definition
- $.fn.hilight = function(options) {
- var defaults = {
- foreground: 'red',
- background: 'yellow'
- };
- // Extend our default options with those provided.
- var opts = $.extend(defaults, options);
- // Our plugin implementation code goes here.
- };
- 我们的插件可以这样被调用:
- $('#myDiv').hilight({
- foreground: 'blue'
- });
// plugin definition $.fn.hilight = function(options) { var defaults = { foreground: 'red', background: 'yellow' }; // Extend our default options with those provided. var opts = $.extend(defaults, options); // Our plugin implementation code goes here. }; 我们的插件可以这样被调用: $('#myDiv').hilight({ foreground: 'blue' });
- // plugin definition
- $.fn.hilight = function(options) {
- // Extend our default options with those provided.
- // Note that the first arg to extend is an empty object -
- // this is to keep from overriding our "defaults" object.
- var opts = $.extend({}, $.fn.hilight.defaults, options);
- // Our plugin implementation code goes here.
- };
- // plugin defaults - added as a property on our plugin function
- $.fn.hilight.defaults = {
- foreground: 'red',
- background: 'yellow'
- };
- 现在使用者可以包含像这样的一行在他们的脚本里:
- //这个只需要调用一次,且不一定要在ready块中调用
- $.fn.hilight.defaults.foreground = 'blue';
- 接下来我们可以像这样使用插件的方法,结果它设置蓝色的前景色:
- $('#myDiv').hilight();
// plugin definition $.fn.hilight = function(options) { // Extend our default options with those provided. // Note that the first arg to extend is an empty object - // this is to keep from overriding our "defaults" object. var opts = $.extend({}, $.fn.hilight.defaults, options); // Our plugin implementation code goes here. }; // plugin defaults - added as a property on our plugin function $.fn.hilight.defaults = { foreground: 'red', background: 'yellow' }; 现在使用者可以包含像这样的一行在他们的脚本里: //这个只需要调用一次,且不一定要在ready块中调用 $.fn.hilight.defaults.foreground = 'blue'; 接下来我们可以像这样使用插件的方法,结果它设置蓝色的前景色: $('#myDiv').hilight();
- // plugin definition
- $.fn.hilight = function(options) {
- // iterate and reformat each matched element
- return this.each(function() {
- var $this = $(this);
- // ...
- var markup = $this.html();
- // call our format function
- markup = $.fn.hilight.format(markup);
- $this.html(markup);
- });
- };
- // define our format function
- $.fn.hilight.format = function(txt) {
- return '<strong>' + txt + '</strong>';
- };
我们很容易的支持options对象中的其他的属性通过允许一个回调函数来覆盖默认的设置。这是另外一个出色的方法来修改你的插件。这里展示的技巧是进一步有效的暴露format函数进而让他能被重新定义。通过这技巧,是其他人能够传递他们自己设置来覆盖你的插件,换句话说,这样其他人也能够为你的插件写插件。
考虑到这个篇文章中我们建立的无用的插件,你也许想知道究竟什么时候这些会有用。一个真实的例子是Cycle插件.这个Cycle插件是一个滑动显示插件,他能支持许多内部变换作用到滚动,滑动,渐变消失等。但是实际上,没有办法定义也许会应用到滑动变化上每种类型的效果。那是这种扩展性有用的地方。 Cycle插件对使用者暴露"transitions"对象,使他们添加自己变换定义。插件中定义就像这样:
$.fn.cycle.transitions = {
- (function($) {
- // plugin definition
- $.fn.hilight = function(options) {
- debug(this);
- // ...
- };
- // private function for debugging
- function debug($obj) {
- if (window.console && window.console.log)
- window.console.log('hilight selection count: ' + $obj.size());
- };
- // ...
- })(jQuery);
(function($) { // plugin definition $.fn.hilight = function(options) { debug(this); // ... }; // private function for debugging function debug($obj) { if (window.console && window.console.log) window.console.log('hilight selection count: ' + $obj.size()); }; // ... })(jQuery);
- $.fn.hilight = function(options) {
- // ...
- // build main options before element iteration
- var opts = $.extend({}, $.fn.hilight.defaults, options);
- return this.each(function() {
- var $this = $(this);
- // build element specific options
- var o = $.meta ? $.extend({}, opts, $this.data()) : opts;
- //...
$.fn.hilight = function(options) { // ... // build main options before element iteration var opts = $.extend({}, $.fn.hilight.defaults, options); return this.each(function() { var $this = $(this); // build element specific options var o = $.meta ? $.extend({}, opts, $this.data()) : opts; //...
这些变动行做了一些事情:它是测试Metadata插件是否被安装如果它被安装了,它能扩展我们的options对象通过抽取元数据这行作为最后一个参数添加到JQuery.extend,那么它将会覆盖任何其它选项设置。现在我们能从"markup”处驱动行为,如果我们选择了“markup”:
- <!-- markup -->
- <div class="hilight { background: 'red', foreground: 'white' }">
- Have a nice day!
- </div>
- <div class="hilight { foreground: 'orange' }">
- Have a nice day!
- </div>
- <div class="hilight { background: 'green' }">
- Have a nice day!
- </div>
- 现在我们能高亮哪些div仅使用一行脚本:
- $('.hilight').hilight();
下面使我们的例子完成后的代码:
- // 创建一个闭包
- (function($) {
- // 插件的定义
- $.fn.hilight = function(options) {
- debug(this);
- // build main options before element iteration
- var opts = $.extend({}, $.fn.hilight.defaults, options);
- // iterate and reformat each matched element
- return this.each(function() {
- $this = $(this);
- // build element specific options
- var o = $.meta ? $.extend({}, opts, $this.data()) : opts;
- // update element styles
- $this.css({
- backgroundColor: o.background,
- color: o.foreground
- });
- var markup = $this.html();
- // call our format function
- markup = $.fn.hilight.format(markup);
- $this.html(markup);
- });
- };
- // 私有函数:debugging
- function debug($obj) {
- if (window.console && window.console.log)
- window.console.log('hilight selection count: ' + $obj.size());
- };
- // 定义暴露format函数
- $.fn.hilight.format = function(txt) {
- return '<strong>' + txt + '</strong>';
- };
- // 插件的defaults
- $.fn.hilight.defaults = {
- foreground: 'red',
- background: 'yellow'
- };
- // 闭包结束
- })(jQuery);
// 创建一个闭包 (function($) { // 插件的定义 $.fn.hilight = function(options) { debug(this); // build main options before element iteration var opts = $.extend({}, $.fn.hilight.defaults, options); // iterate and reformat each matched element return this.each(function() { $this = $(this); // build element specific options var o = $.meta ? $.extend({}, opts, $this.data()) : opts; // update element styles $this.css({ backgroundColor: o.background, color: o.foreground }); var markup = $this.html(); // call our format function markup = $.fn.hilight.format(markup); $this.html(markup); }); }; // 私有函数:debugging function debug($obj) { if (window.console && window.console.log) window.console.log('hilight selection count: ' + $obj.size()); }; // 定义暴露format函数 $.fn.hilight.format = function(txt) { return '<strong>' + txt + '</strong>'; }; // 插件的defaults $.fn.hilight.defaults = { foreground: 'red', background: 'yellow' }; // 闭包结束 })(jQuery);
jQuery.extend(object); 为扩展jQuery类本身.为类添加新的方法。