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. Hive_DDL与DML

    DDL(数据定义语言) create.drop.alter.truncate.show.describe DML(数据控制语言) load.insert.update.delete.import/ex ...

  2. Odoo SSO 单点登录

    很多公司会有内部单点登录系统,采用Odoo系统的公司可能就有需要将Odoo接入公司内部的单点登录系统. 实现的思路很简单,由于每个公司的系统不一样,代码仅作示例说明. 首先,重写Odoo登录界面: & ...

  3. JQuery 遍历子元素+ each函数的跳出+提取字符串中的数字

    最近脑袋迷糊的如同一团浆糊,一直出错. HTML代码如下图,现在想实现的功能是根据Ajax请求,获取到具体的button,以更新其样式.由于Button较多,每个Button都设置id,没有意义,想通 ...

  4. 【HDU4612】 双连通分量求桥

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4612 题目大意:给你一个无向图,问你加一条边后最少还剩下多少多少割边. 解题思路:好水的一道模板题.先 ...

  5. oracle 字符乱码问题解决方案

    今天在客户服务器上遇到了oracle中文乱码问题,第一个想到的是:要想避免oracle字符乱码的问题,需要注意oracle客户端的字符编码和服务端的字符编码保持一致. 于是操作如下: 1.查看服务端字 ...

  6. 网页mp3播放代码

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. Xcode 添加类前缀

    按照如下图所示操作后,接下来创建的类就会带有MN的前缀;如果想更换前缀,则替换MN即可!

  8. MyEclipse修改项目名称后,部署到tomcat问题

    问题描述: 修改项目名称后,部署到tomcat server,部署出来的文件夹名还是旧的名称. 解决方案: 光把项目重命名是不够的,还要修改一下Myeclipse里面的配置. 工程名->右键-& ...

  9. Unity学习疑问记录之新GUI

    学习Unity 4.6新GUI系统 http://segmentfault.com/a/1190000000642686

  10. nginx服务器状态监控

    Nginx开启监控需在编译时加入with-http_stub_status_module,查看当前Nginx编译参数:/usr/local/nginx/sbin/nginx -V 1.以二级目录方式开 ...