Writing Your Own jQuery Plugins

Setting Up

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

The jQuery Plugin Structure

1 (function($) {
2
3     $.fn.helloWorld = function() {
4
5         // Future home of "Hello, World!"
6
7     }
8
9 }(jQuery));

Making Our Plugin Do Something

 1 (function($) {
 2
 3     $.fn.helloWorld = function() {
 4
 5         this.each( function() {
 6             $(this).text("Hello, World!");
 7         });
 8
 9     }
10
11 }(jQuery));

Invoke the plugin

1 <script>
2 $(document).ready( function() {
3     $(‘h2‘).helloWorld();
4 });
5 </script>

Return the results of the plugin(necessary)

 1 (function($) {
 2
 3     $.fn.helloWorld = function() {
 4
 5         return this.each( function() {
 6             $(this).text("Hello, World!");
 7         });
 8
 9     }
10
11 }(jQuery));

But Wait, There’s More!

 1 (function($) {
 2
 3     $.fn.helloWorld = function( customText ) {
 4
 5         return this.each( function() {
 6             $(this).text( customText );
 7         });
 8
 9     }
10
11 }(jQuery));

Invoke the plugin

1 <script>
2 $(document).ready( function() {
3     $(‘h2‘).helloWorld(‘¡Hola, mundo!‘);
4 });
5 </script>

Complete Customization

 1 (function($){
 2     //
 3     $.fn.helloWorld = function(options){
 4
 5         var settings = $.extend({
 6             text: "hello girl!",
 7             fontSize: null,
 8             color: null,
 9         },options);
10
11         return this.each(function(){
12             $(this).text(settings.text);
13             if(settings.fontSize != null){
14                 $(this).css("font-size",settings.fontSize);
15             }
16             if(settings.color != null){
17                 $(this).css("color",settings.color);
18             }
19         });
20
21     }
22
23 }(jQuery));

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

 1 (function($){
 2     //
 3     $.fn.helloWorld = function(options){
 4
 5         var settings = $.extend({
 6             text: "hello girl!",
 7             fontSize: null,
 8             color: null,
 9             complete: function(){ alert("Done!");}
10         },options);
11
12         return this.each(function(){
13             $(this).text(settings.text);
14             if(settings.fontSize != null){
15                 $(this).css("font-size",settings.fontSize);
16             }
17             if(settings.color != null){
18                 $(this).css("color",settings.color);
19             }
20             if($.isFunction(settings.complete)){
21                 settings.complete.call(this);
22             }
23
24         });
25
26     }
27
28 }(jQuery));

On the invocation side, our code becomes:

1 $(‘h2‘).helloWorld({
2     text        : ‘Salut, le monde!‘,
3     color       : ‘#005dff‘,
4     fontStyle   : ‘italic‘,
5     complete    : function() { alert( ‘Done!‘ ) }
6 });

原文地址:Writing Your Own jQuery Plugins

时间: 2024-08-07 08:38:47

Writing Your Own jQuery Plugins的相关文章

jQuery plugins

http://jquery.com/ http://jqueryui.com/ http://www.jqueryrain.com/ http://plugins.jquery.com/ http://www.jqueryscript.net/ http://jqueryhouse.com/

jQuery plugins 图片上传

http://plugins.jquery.com/ 用到一下插件: magnific popup 看大图 jQuery File Upload 多文件上传 jQuery Rotate 图片旋转 github 项目地址: [email protected]:witaste/imgupload.git 效果截图:

Best jQuery Plugins of the Month – May 2014

1. jQuery referenceSection jQuery referenceSection by Scott Mascio ensures to help users in adding a reference section to page along with the description of each selected elements that can be seen on hovering. Demo | Download 2. Address Field Address

Jquery Plugins Jquery Validate

  Jquery Validate 一.什么是Jquery Validate: jQuery Validate 插件为表单提供了强大的验证功能. 二.常用值: 1 required:true 必须输入的字段. 2 remote:"check.php" 使用 ajax 方法调用 check.php 验证输入值. 3 email:true 必须输入正确格式的电子邮件. 4 url:true 必须输入正确格式的网址. 5 date:true 必须输入正确格式的日期.日期校验 ie6 出错,慎

jquery plugins —— datatables 搜索后汇总

网上的例子 http://datatables.club/example/plug-ins/api.html只能对当前页面或所有数据进行汇总,不能对搜索结果数据汇总. 以下是对datatables自带搜索结果的汇总. table.on("search.dt", dataSearch); //汇总总金额 function dataSearch() { var datas = table.data(); var total = 0; for (i = 0; i < table.con

jquery plugins —— datatables 增加行号

table = $("#Table").DataTable({ "rowCallback": function (row, data, dataIndex) { //增加行号 $('td:eq(0)', row).html(dataIndex + table.page.info().length * table.page() + 1); }}); <table id="Table"> <thead> <tr styl

jQuery Plugins Validate

validate报错 1 //onfocusout默认为true,但是加上会报错. 2 //官方文档解释: 3 //A boolean true is not a valid value. 4 $("#test").validate({ 5 debug:true, 6 onfocusout:true, 7 rules: { 8 testName: "required", 9 }, 10 messages: { 11 testName: { required: &qu

Learning JavaScript Design Patterns -- A book by Addy Osmani

Learning JavaScript Design Patterns A book by Addy Osmani Volume 1.6.2 Tweet Copyright © Addy Osmani 2015. Learning JavaScript Design Patterns is released under a Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 unported license. It

使用 jQuery UI Widget Factory 编写有状态的插件(Stateful Plugins)

使用 jQuery UI Widget Factory 编写有状态的插件(Stateful Plugins) Note 这一章节的内容是基于 Scott Gonzalez 一篇博客 Building Stateful jQuery Plugins(已获作者许可) 虽然大多数的 jQuery 插件都是无状态的(stateless),也就是说, 与插件进行交互的就限于调用插件时的那一组对象, 但是有好大一部分功能需求没办法通过这种简单的插件模式来实现. 为了填补这一空白,jQuery UI 实现一套