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. 已知树的前序、中序,求后序的c++实现&已知树的后序、中序,求前序的c++实现

    #include"iostream" using namespace std; int pre[30]; int in[30]; int post[30]; int indexOf ...

  2. 做为一名dba你应该知道这些数据恢复

    1.将备份数据   拉取到本地虚拟机上 进行恢复(千万不要把数据直接恢复到生产中,除非迫不得已!!)   2.在本地虚拟机上恢复之后,导出需要恢复的数据.   3.在本地虚拟机上恢复做恢复测试.   ...

  3. jquery过滤器

    <html> <head> <meta charset="UTF-8"> <title>Document</title> ...

  4. bzoj3732: Network--kruskal最小生成树+LCA

    这是一道写起来比较顺手的题目 没有各种奇怪的细节,基本就是Kruskal和倍增LCA的模板.. 题目大意:对于一个无向带权图,询问两点之间一条路,使得这条路上的最长边最小,输出最小最长边的的值 那么既 ...

  5. 基于Jenkins的环境搭建

    基于 Jenkins 快速搭建持续集成环境 持续集成是一种软件开发实践,对于提高软件开发效率并保障软件开发质量提供了理论基础.Jenkins 是一个开源软件项目,旨在提供一个开放易用的软件平台,使持续 ...

  6. pch头文件

    1.command+N ---> Other ---> PCH File 2.点击工程 ---> Build Settings ---> 搜索框中输入pref ---> ...

  7. Handler的总结

    Handler的总结 我们创建的Service.Activity,Broadcast均是一个主线程处理,即UI线程, 但是进行耗时操作时,比如I/O读写的大文件,数据库操作及网络下载需要很长的时间,为 ...

  8. 【转】Eclipse 常用快捷键 (动画讲解)

    Eclipse有强大的编辑功能, 工欲善其事,必先利其器, 掌握Eclipse快捷键,可以大大提高工作效率. 小坦克我花了一整天时间, 精选了一些常用的快捷键操作,并且精心录制了动画, 让你一看就会. ...

  9. Web Api 中Get 和 Post 请求的多种情况分析

    转自:http://www.cnblogs.com/babycool/p/3922738.html 来看看对于一般前台页面发起的get和post请求,我们在Web API中要如何来处理. 这里我使用J ...

  10. 回归测试---junit

    回归测试是指修改了旧代码后,重新进行测试以确认修改没有引入新的错误或导致其他代码产生错误. JUnit是一个Java语言的单元测试框架. http://blog.csdn.net/andycpp/ar ...