Writing Your Own jQuery Plugins
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
Writing Your Own jQuery Plugins的更多相关文章
- 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 ...
- jQuery plugins 图片上传
http://plugins.jquery.com/ 用到一下插件: magnific popup 看大图 jQuery File Upload 多文件上传 jQuery Rotate 图片旋转 gi ...
- Best jQuery Plugins of the Month – May 2014
1. jQuery referenceSection jQuery referenceSection by Scott Mascio ensures to help users in adding a ...
- jquery plugins
jQuery官网插件 jQuery自定义滚动条样式插件 jQuery custom content scroller examples Twitter typeahead typeahead.js t ...
- jquery plugins —— datatables 搜索后汇总
网上的例子 http://datatables.club/example/plug-ins/api.html只能对当前页面或所有数据进行汇总,不能对搜索结果数据汇总. 以下是对datatables自带 ...
- Jquery Plugins Jquery Validate
Jquery Validate 一.什么是Jquery Validate: jQuery Validate 插件为表单提供了强大的验证功能. 二.常用值: 1 required:true 必须输入 ...
- jquery plugins —— datatables 增加行号
table = $("#Table").DataTable({ "rowCallback": function (row, data, dataIndex) { ...
- jquery plugins —— datatables ajax post更新数据
通过下面语句,可以定义datatables插件通过ajax post方法从服务器段获取JSON格式的数据. 错误写法(这样写再执行ajax.reload()方法时,ID参数还是初始时,不会更新): v ...
- jquery plugins —— Chosen
官网地址:http://harvesthq.github.io/chosen/ Chosen (v1.4.2) Chosen has a number of options and attribute ...
随机推荐
- [LintCode] Reverse Linked List 倒置链表
Reverse a linked list. Have you met this question in a real interview? Yes Example For linked list 1 ...
- 解决mysql5.6+在zabbix监控中执行脚本出现密码的错误问题
1.mysql命令行中授权mysql监控所需的账号和密码(权限select权限即可) 2.通过mysql_config_editor 配置登录问题: [root@back_zabbix_100 scr ...
- Scala命令设置JVM参数的规则
Scala下设置JVM参数简单分析 Scala 启动shell脚本,简化后的scala REPL 启动命令大致如下所示: java -Xmx256M -Xms32M \-Xbootclasspath/ ...
- Lamp下安全配置随笔
Apache方面: 1.apache有两个指令可以输出服务器的细节,即ServerSignature和ServerTokens. 当这两个指令一起使用时,会输出apache的版本号,php的版本号,i ...
- 示例说明Oracle RMAN两种库增量备份的差别
1差异增量实验示例 1.1差异增量备份 为了演示增量备份的效果,我们在执行一次0级别的备份后,对数据库进行一些改变. 再执行一次1级别的差异增量备份: 执行完1级别的备份后再次对数据库进行更改: 再执 ...
- 类似input框内最右边添加图标,有清空功能
<html> <head> <meta http-equiv="Content-Type" content="text/html; char ...
- remove 清除binlog
#!/bin/bash DATACFG=/etc/my.cnf DATADIR=`awk /^datadir/ $DATACFG|awk -F"=" '{print $2}'` D ...
- Java语法基础思维图
- IOS第16天(4,Quartz2D柱状图)
*** #import "HMBarView.h" #import "UIColor+Random.h" @implementation HMBarView - ...
- Windows7如何安装Sqlite3
Sqlite官网地址:http://www.sqlite.org/ Sqlite3文件下载: 1.下载 sqlite-dll-win32-x86-3140100 2.下载 sqlite-tools-w ...