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 ...
随机推荐
- load/get延迟加载和及时加载
load和get方法的区别: Session.load/get方法均可以根据指定的实体类和id从数据库读取记录,并返回与之对应的实体对象. 区别在于: 如果未能发现符合条件的记录,get方法返回nul ...
- [CareerCup] 17.8 Contiguous Sequence with Largest Sum 连续子序列之和最大
17.8 You are given an array of integers (both positive and negative). Find the contiguous sequence w ...
- 内网网段划分ciso交换机配置
内网3750交换机配置: vlan 192 192.168.101.0/24 端口: 1--8vlan 10 10.10.10.0/24 端口: 9--16vlan 172 172.16.172.2/ ...
- 获取Java系统相关信息
package com.test; import java.util.Properties; import java.util.Map.Entry; import org.junit.Test; pu ...
- 在HTML文件的表单中添加{%csrf_token%}便可以解决问题
原因是django为了在用户提交表单时防止跨站攻击所做的保护 只需在HTML文件的表单中添加{%csrf_token%}便可以解决问题 ------------------------if判断{% i ...
- Android课程---计算器的实现
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="ht ...
- IOS第18天(8,核心动画转场动画)
***翻页效果 #import "HMViewController.h" @interface HMViewController () @property (weak, nonat ...
- windows7系统下一些常用工具的总结
1.查看计算机的基本信息:计算机--右键--属性(快捷键:Win+Pause) 2.查看计算机的详细信息:开始菜单--所有程序--附件--系统工具--系统信息(运行命令:msinfo32) 3.定制计 ...
- DevExpress GridView对表格的部分说明
1. int[] selects = this.m_grdView1.GetSelectedRows(); // 获取选中的行,可能是几行 2. this.m_grdView1.GetRowCellV ...
- IIS应用程序池最大进程数设置
1.当工作进程数>1时,如果有多个连接请求就会启动多个工作进程实例来处理,所启动的最多工作进程数就是你设置的最大进程数,后续更多的连接请求会循环的发送至不同的工作进程来处理.每个工作进程都能承担 ...