Setting Up

 <script src="js/jquery-1.9.1.min.js"></script>
<script src="js/jquery.hello-world.js"></script>

The jQuery Plugin Structure

 (function($) {

     $.fn.helloWorld = function() {

         // Future home of "Hello, World!"

     }

 }(jQuery));

Making Our Plugin Do Something

 (function($) {

     $.fn.helloWorld = function() {

         this.each( function() {
$(this).text("Hello, World!");
}); } }(jQuery));

Invoke the plugin

 <script>
$(document).ready( function() {
$('h2').helloWorld();
});
</script>

Return the results of the plugin(necessary)

 (function($) {

     $.fn.helloWorld = function() {

         return this.each( function() {
$(this).text("Hello, World!");
}); } }(jQuery));

But Wait, There’s More!

 (function($) {

     $.fn.helloWorld = function( customText ) {

         return this.each( function() {
$(this).text( customText );
}); } }(jQuery));

Invoke the plugin

 <script>
$(document).ready( function() {
$('h2').helloWorld('¡Hola, mundo!');
});
</script>

Complete Customization

 (function($){
//
$.fn.helloWorld = function(options){ var settings = $.extend({
text: "hello girl!",
fontSize: null,
color: null,
},options); return this.each(function(){
$(this).text(settings.text);
if(settings.fontSize != null){
$(this).css("font-size",settings.fontSize);
}
if(settings.color != null){
$(this).css("color",settings.color);
}
}); } }(jQuery));

Use a “complete” variable to perform an action when our plugin completes its action.

 (function($){
//
$.fn.helloWorld = function(options){ var settings = $.extend({
text: "hello girl!",
fontSize: null,
color: null,
complete: function(){ alert("Done!");}
},options); return this.each(function(){
$(this).text(settings.text);
if(settings.fontSize != null){
$(this).css("font-size",settings.fontSize);
}
if(settings.color != null){
$(this).css("color",settings.color);
}
if($.isFunction(settings.complete)){
settings.complete.call(this);
} }); } }(jQuery));

On the invocation side, our code becomes:

 $('h2').helloWorld({
text : 'Salut, le monde!',
color : '#005dff',
fontStyle : 'italic',
complete : function() { alert( 'Done!' ) }
});

原文地址:Writing Your Own jQuery Plugins

jQuery 官网实例

Writing Your Own jQuery Plugins的更多相关文章

  1. jsDelivr - Free open source CDN for javascript libraries, jQuery plugins, CSS frameworks, Fonts and more

    jsDelivr - Free open source CDN for javascript libraries, jQuery plugins, CSS frameworks, Fonts and ...

  2. jQuery plugins 图片上传

    http://plugins.jquery.com/ 用到一下插件: magnific popup 看大图 jQuery File Upload 多文件上传 jQuery Rotate 图片旋转 gi ...

  3. Best jQuery Plugins of the Month – May 2014

    1. jQuery referenceSection jQuery referenceSection by Scott Mascio ensures to help users in adding a ...

  4. jquery plugins

    jQuery官网插件 jQuery自定义滚动条样式插件 jQuery custom content scroller examples Twitter typeahead typeahead.js t ...

  5. jquery plugins —— datatables 搜索后汇总

    网上的例子 http://datatables.club/example/plug-ins/api.html只能对当前页面或所有数据进行汇总,不能对搜索结果数据汇总. 以下是对datatables自带 ...

  6. Jquery Plugins Jquery Validate

      Jquery Validate 一.什么是Jquery Validate: jQuery Validate 插件为表单提供了强大的验证功能. 二.常用值: 1 required:true 必须输入 ...

  7. jquery plugins —— datatables 增加行号

    table = $("#Table").DataTable({ "rowCallback": function (row, data, dataIndex) { ...

  8. jquery plugins —— datatables ajax post更新数据

    通过下面语句,可以定义datatables插件通过ajax post方法从服务器段获取JSON格式的数据. 错误写法(这样写再执行ajax.reload()方法时,ID参数还是初始时,不会更新): v ...

  9. jquery plugins —— Chosen

    官网地址:http://harvesthq.github.io/chosen/ Chosen (v1.4.2) Chosen has a number of options and attribute ...

随机推荐

  1. val, lazy, def

    val strVal = scala.io.Source.fromFile("test.txt").mkString //在strVal被定义的时候获取值,如果test.txt不存 ...

  2. 不同版本strtotime("2016-09-04")输出不同问题

    在php版本改变后,突然发现查询没结果.然后发现是时间不同了,测试以后发现以前版本的php和现在版本的phpstrtotime输出不同.原来是时区配置问题,解决办法:http://zhidao.bai ...

  3. linux笔记七---------管道

    smarty的变量调节器就是linux的管道 管道:前者的输出是后者的输入 {$name|upper}   通过调节器使得名字变为大写输出 {$name|lower} linux的管道: ls –al ...

  4. 表单的enctype property

    enctype 属性规定在发送到服务器之前应该如何对表单数据进行编码. 默认地,表单数据会编码为 "application/x-www-form-urlencoded".就是说,在 ...

  5. 关于Java擦除特性

    package thinkingInJava; /* * 模拟擦除 */ public class SimpleHolder { private Object obj ; public void se ...

  6. A trip through the Graphics Pipeline 2011_09_Pixel processing – “join phase”

    Welcome back!    This post deals with the second half of pixel processing, the “join phase”. The pre ...

  7. 使用Visual Studio下自带的SQL Server Express

    软件环境:Windows7(x64) + Visual Studio 2010 + SQL Server Express 2008 1.配置数据库 装VS2010不小心把自带的SQL Server 2 ...

  8. 在Altium_Designer_PCB_中插入图片的方法

    详细请看PDF: http://files.cnblogs.com/files/BinB-W/在Altium_Designer_PCB_中插入图片的方法.pdf 配套文件: http://files. ...

  9. 【转】PHP实现连贯操作

    [第一种方案 __call] 我们在使用一些框架(如ThinkPHP)编码的时候,常用到这样的代码. M('User')->where(array('id'=>1))->field( ...

  10. 【转】HTTP 头部解释,HTTP 头部详细分析,最全HTTP头部信息

    HTTP 头部解释 ========================================================================================== ...