`
bolv88
  • 浏览: 1440 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
社区版块
存档分类
最新评论

jquery 插件

阅读更多

1. 最终效果

// Shawn Khameneh
// ExtraordinaryThoughts.com
 
(function($) {
	var privateFunction = function() {
		// code here
	}
 
	var methods = {
		init: function(options) {
			return this.each(function() {
				var $this = $(this);
				var settings = $this.data('pluginName');
 
				if(typeof(settings) == 'undefined') {
 
					var defaults = {
						propertyName: 'value',
						onSomeEvent: function() {}
					}
 
					settings = $.extend({}, defaults, options);
 
					$this.data('pluginName', settings);
				} else {
					settings = $.extend({}, settings, options);
				}
 
				// run code here
 
			});
		},
		destroy: function(options) {
			return $(this).each(function() {
				var $this = $(this);
 
				$this.removeData('pluginName');
			});
		},
		val: function(options) {
			var someValue = this.eq(0).html();
 
			return someValue;
		}
	};
 
	$.fn.pluginName = function() {
		var method = arguments[0];
 
		if(methods[method]) {
			method = methods[method];
			arguments = Array.prototype.slice.call(arguments, 1);
		} else if( typeof(method) == 'object' || !method ) {
			method = methods.init;
		} else {
			$.error( 'Method ' +  method + ' does not exist on jQuery.pluginName' );
			return this;
		}
 
		return method.apply(this, arguments);
 
	}
 
})(jQuery);

 

2. 基本框架

(function($) {
 
	$.fn.pluginName = function(options) {
 
		// code here
                alert("init");
		return this;
	}
  

})(jQuery);

 

短短几行代码, 就写完了一个框架, 之后你可以这样调用: $(‘#element’).pluginName() 

 

3. 多元素选择

 

(function($) {
 
	// Add the plugin to the jQuery (protected, "fn") namespace under "pluginName" as the function name.
	$.fn.pluginName = function(options) {
 
		// Return "this" (result of "each" is also "this") to allow for chaining the plugin
		return this.each(function() {
 
			// code here, reference each individual element by "this"
			// e.g. $(this).show();
			var $this = $(this);
                         ....init($this);
		});
 
	}
 
})(jQuery);

jQuery 中我们经常用 $(".class") 等类似的写法 来选择多个元素, 所以我们插件可以通过each 循环的方式来为每个元素初始化

 

4. 为插件添加共有私有方法

(function($) {
 
	// Create a private function by creating a public variable within our plugin's container.
	var privateFunction = function() {
		// code here
	}
 
	// Create an object literal for our methods
	var methods = {
		// Define individual methods within the literal
		init: function() {
 
			// Repeat over each element in selector
			// Taken from our main function and moved into each method for flexibility
			return this.each(function() {
				// Create a jQuery object to use with this individual element
				var $this = $(this);
 
				// run code here
				// e.g. privateFunction();
			});
		},
		destroy: function() {
			// Repeat over each element in selector
			return this.each(function() {
				// run code here
			});
		}
	};
 
	$.fn.pluginName = function() {
		// Grab our method, sadly if we used function(method){}, it ruins it all
		var method = arguments[0];
 
		// Check if the passed method exists
		if(methods[method]) {
 
			// If the method exists, store it for use
			// Note: I am only doing this for repetition when using "each()", later.
			method = methods[method];
 
		// If the method is not found, check if the method is an object (JSON Object) or one was not sent.
		} else if( typeof(method) == 'object' || !method ) {
 
			// If we passed parameters as the first object or no arguments, just use the "init" methods
			method = methods.init;
		} else {
 
			// Not a method and not parameters, so return an error.  Something wasn't called correctly.
			$.error( 'Method ' +  method + ' does not exist on jQuery.pluginName' );
			return this;
		}
 
		// Call our selected method
		// Once again, note how we move the "each()" from here to the individual methods
		return method.call(this);
 
	}
 
})(jQuery);

 

methods  中的方法为共有方法外界可以调用, privateFunction 为私有方法,外界无法触及

调用示例:

$('.className').pluginName();
$('.className').pluginName('init');
$('.className').pluginName('init', {}); // Pass object "{}" to "init" as arguments
$('.className').pluginName({}); // Pass object "{}" to "init" as arguments
 
// Call method "destroy" on each element matching ".className"
$('.className').pluginName('destroy');
$('.className').pluginName('destroy', {}); // Pass object "{}" to "destroy" as arguments
 

5. 调用插件时传递参数

(function($) {
	var methods = {
		init: function(options) {
 
			// Repeat over each element in selector
			return this.each(function() {
				var $this = $(this);
 
				// Create a default settings variable
				var defaults = {
					propertyName: 'value',
					onSomeEvent: function() {}
				}
 
				// Use extend to create settings from passed options and the defaults
				var settings = $.extend({}, defaults, options);
 
				// run code here
 
			});
		}
	};
 
	$.fn.pluginName = function() {
		var method = arguments[0];
 
		if(methods[method]) {
			method = methods[method];
 
			// Our method was sent as an argument, remove it using slice because it's not an argument for our method
			arguments = Array.prototype.slice.call(arguments, 1);
		} else if( typeof(method) == 'object' || !method ) {
			method = methods.init;
		} else {
			$.error( 'Method ' +  method + ' does not exist on jQuery.pluginName' );
			return this;
		}
 
		// Use apply to sent arguments when calling our selected method
		return method.apply(this, arguments);
 
	}
 
})(jQuery);

 

6. 保存插件初始化参数

init: function(options) {
 
			// Repeat over each element in selector
			return this.each(function() {
				var $this = $(this);
 
				// Attempt to grab saved settings, if they don't exist we'll get "undefined".
				var settings = $this.data('pluginName');
 
				// If we could't grab settings, create them from defaults and passed options
				if(typeof(settings) == 'undefined') {
 
					var defaults = {
						propertyName: 'value',
						onSomeEvent: function() {}
					}
 
					settings = $.extend({}, defaults, options);
 
					// Save our newly created settings
					$this.data('pluginName', settings);
				} else {
					// We got settings, merge our passed options in with them (optional)
					settings = $.extend({}, settings, options);
 
					// If you wish to save options passed each time, add:
					// $this.data('pluginName', settings);
				}
 
				// run code here
 
			});
		},

通过这一句: $this.data('pluginName', settings); 就将参数绑定在了元素上, 之后可以通过$(element).data('pluginName') 方式来获取

 

over

内容来源:

http://extraordinarythoughts.com/2011/08/20/understanding-jquery-plugins/

 

分享到:
评论

相关推荐

    jquery插件库大全(200个).zip

    jquery插件库大全(200个): jqueryQQ表情插件 jquery下拉菜单导航 jquery下拉菜单栏 jquery仿Windows系统选中图标效果 jquery仿京东商品详情页图片放大效果 jquery仿百度新闻焦点轮播 jquery分离布局模版 jquery...

    jQuery插件jQuery插件

    jQuery插件jQuery插件jQuery插件jQuery插件jQuery插件jQuery插件jQuery插件jQuery插件jQuery插件jQuery插件jQuery插件jQuery插件jQuery插件

    jquery插件库-jquery仿京东商品详情页图片放大效果.zip

    "jquery插件库-jquery仿京东商品详情页图片放大效果.zip"这个压缩包文件包含了一个jQuery插件,旨在模仿京东商品详情页中的图片放大效果,为用户提供更直观的商品查看体验。 首先,我们来详细了解一下jQuery插件的...

    好用清除html的jquery插件

    你可以通过查看源代码来学习其工作原理,这对于初学者或者希望深入了解jQuery插件开发的人来说是一个很好的学习机会。 标签“清除html格式”是这个插件的核心特性,它可能包括移除HTML标签、属性、样式以及其他非...

    jquery插件.rar

    《jQuery插件开发详解》 在Web开发领域,jQuery是一个广泛应用的JavaScript库,它极大地简化了JavaScript的DOM操作、事件处理、动画制作等任务。jQuery插件是jQuery生态系统的重要组成部分,它们扩展了jQuery的核心...

    JQuery插件

    JQuery插件是JavaScript库JQuery生态中的一个重要组成部分,它扩展了JQuery的基本功能,为开发者提供了更加丰富和便捷的API来实现各种复杂的交互效果和动画。在网页开发中,JQuery插件广泛应用于增强用户体验、创建...

    jquery插件

    2. **多媒体播放**:jQuery插件如`jQuery.media`和`jPlayer`提供了优雅的方式来处理视频和音频播放,支持多种格式,提供自定义的播放器皮肤和控制选项,使开发者能够创建互动性强的多媒体体验。 3. **DOM操作和事件...

    jquery插件库(jquery.treeview插件库)

    jquery插件库(jquery.treeview插件库)jquery插件库(jquery.treeview插件库)jquery插件库(jquery.treeview插件库)jquery插件库(jquery.treeview插件库)jquery插件库(jquery.treeview插件库)jquery插件库(jquery....

    jquery 插件jquery 插件jquery 插件jquery 插件

    jQuery插件是jQuery库的扩展,它为开发者提供了丰富的功能,使得JavaScript编程更为简便和高效。jQuery本身提供了基础的DOM操作、事件处理、动画效果以及Ajax交互等核心功能,但为了满足更复杂的需求,开发者们创建...

    50个精彩JQuery插件案例

    1. **jQuery插件的原理**:jQuery插件是基于jQuery核心功能扩展的功能模块,通过$.fn.extend()方法,将新方法添加到jQuery对象的prototype上,使得所有jQuery选择器都能调用这些新方法。 2. **DOM操作**:jQuery...

    java源代码,主要是jquery插件的利用

    本主题聚焦于"java源代码,主要是jquery插件的利用",这意味着我们将探讨如何在Java后端项目中集成与使用jQuery插件,以及相关的编程实践。 首先,我们需要理解jQuery是一个JavaScript库,它通过提供简洁的API来...

    50个jquery插件介绍

    "50个jQuery插件介绍"这篇文档很可能包含了对各种功能强大、用途广泛的jQuery插件的详细解读,帮助开发者提升网站的用户体验和功能实现。 首先,jQuery插件是jQuery库的扩展,由社区开发者创建,用于解决特定问题或...

    sublime2 jquery插件

    在Sublime Text 2中安装jQuery插件,可以极大地提升前端开发者的工作效率,使得jQuery代码的编写更加得心应手。 jQuery是一种强大的JavaScript库,它简化了HTML文档遍历、事件处理、动画以及Ajax交互等任务,极大地...

    jquery插件大全(内含Demo)

    **jQuery插件大全:打造网页交互新体验** jQuery是一个广泛使用的JavaScript库,它极大地简化了JavaScript的DOM操作、事件处理、动画设计以及Ajax交互。这个"jQuery插件大全(内含Demo)"压缩包文件提供了丰富的...

    jQuery插件开发的五种形态小结

    jQuery作为JavaScript领域中广泛使用的库之一,提供了快速、简洁的DOM操作和事件处理方式,而jQuery插件则是扩展jQuery功能的重要手段。本知识点将详细介绍如何开发jQuery插件,并总结五种常见的形态。 ### 一、...

    eclipse上支持jquery插件

    标题“eclipse上支持jquery插件”所指的就是这种能够为Eclipse添加jQuery智能提示和代码辅助功能的扩展。这样的插件使得开发者在编写jQuery代码时,可以享受到如同使用Java或其他语言一样的代码补全、错误检查和调试...

    很好用的Jquery插件

    本文将深入探讨“很好用的Jquery插件”,并介绍几个常用插件及其简单使用方法,这对于在项目中提升用户体验具有显著帮助。 首先,jQuery库的核心优势在于它的API设计,它提供了丰富的选择器、DOM操作、事件处理和...

    eclipse中jquery插件配置

    Eclipse 中 jQuery 插件配置详解 在 Eclipse 中配置 jQuery 插件可以实现智能提示功能,提高编码效率。下面详细介绍了 jQuery 插件配置的步骤和注意事项。 Eclipse 中 jQuery 插件配置步骤 1. 下载 jQueryWTP ...

Global site tag (gtag.js) - Google Analytics