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] Swap Nodes in Pairs 成对交换节点
Given a linked list, swap every two adjacent nodes and return its head. Example Given 1->2-> ...
- hadoop配置远程客户端
独立出一台机器,作为客户端,可以连接远程集群,配置注意事项: 1.首先是hive,需要服务器启动一个服务 hive --service metastore 然后修改hive客户端 hive-site. ...
- android-WebView
使用WebView 加载页面 一.加载页面 使用loadUrl() web资源:webView.loadUrl("http://www.baidu.com"); 本地文件用:web ...
- JS 心得总结
1.浏览器关闭事件监听(http://pengjianbo1.iteye.com/blog/507569,http://bbs.csdn.net/topics/360152711) <!DOCT ...
- 安装redis时遇到zmalloc.h:50:31: 致命错误:jemalloc/jemalloc.h:没有那个文件或目录
参考博文,http://www.phperz.com/article/14/1219/42002.html 解决办法 make MALLOC=libc
- sqlmap 1.0.21 tamper 总结
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- javascript平时小例子④(setInterval使用2)
<!DOCTYPE html><html> <head> <meta charset="utf-8"> <title>& ...
- 2016HUAS暑假集训训练2 F - A Simple Problem with Integers
Description 给出了一个序列,你需要处理如下两种询问. "C a b c"表示给[a, b]区间中的值全部增加c (-10000 ≤ c ≤ 10000). " ...
- IP地址的分类与寻址
IP地址:有一种标识符,被TCP/IP协议簇的IP层用来标识 连接到因特网的设备.IP协议的第4版IPv4地址是32位地址,是连接地址,定义了每一个连接到因特网上的设备(可以认为是主机的别名),而不是 ...
- 温故而知新 OOP
设计原则1: 找出应用中可能需要发生改变的地方,把它们独立出来,不要和那些不需要变化的代码混在一起换句话说,如果每次新的需求一来,都会使某方面的代码发生变化,此时你就可以确定,这部分代码属于不稳定代码 ...