jQuery改造插件,添加回调函数
<script language="javascript" type="text/javascript">
function doSomething(callback) {
// …
// Call the callback
callback('stuff', 'goes', 'here'); // 给callback赋值,callback是个函数变量
} function foo1(a, b, c) {
// I'm the callback
alert(a + " " + b + " " + c);
}
doSomething(foo1); // foo1函数将使用callback函数中的数据 stuff goes here var foo2 = function(a,b,c) {
// I'm the callback
alert(a + " " + b + " " + c);
}
doSomething(foo2); // foo2函数将使用callback函数中的数据 stuff goes here doSomething(function(a,b,c){
alert(a + " " + b + " " + c); // function函数将使用callback函数中的数据 stuff goes here
});
</script>
callback这个参数必须是函数才有效。才能起到回调的作用。
<script language="javascript" type="text/javascript">
function doSomething(callback) {
// …
// Call the callback
if(typeof callback === 'function'){
callback('stuff', 'goes', 'here'); // 给callback赋值,callback是个函数变量
}else{
alert('fuck you');
}
} function foo1(a, b, c) {
// I'm the callback
alert(a + " " + b + " " + c);
}
doSomething(foo1); // foo1函数将使用callback函数中的数据 stuff goes here var foo2 = function(a,b,c) {
// I'm the callback
alert(a + " " + b + " " + c);
}
doSomething(foo2); // foo2函数将使用callback函数中的数据 stuff goes here doSomething(function(a,b,c){
alert(a + " " + b + " " + c); // function函数将使用callback函数中的数据 stuff goes here
});
var foo3 = 'a';
doSomething(foo3);
</script>
foo3不是函数的时候,弹出fuck you
jQuery实例
原函数
$.fn.citySelect=function(settings)
添加回调
$.fn.citySelect=function(settings, changeHandle) // 添加回调函数changeHandle
给回调函数赋值
//选项变动赋值事件
var selectChange = function (areaType) {
if(typeof changeHandle === 'function'){ // 判断callback是否是函数
var prov_id = prov_obj.get(0).selectedIndex;
var city_id = city_obj.get(0).selectedIndex;
var dist_id = dist_obj.get(0).selectedIndex; if(!settings.required){
prov_id--;
city_id--;
dist_id--;
}; if(dist_id<0){
var data = {
prov: city_json.citylist[prov_id].p,
city: city_json.citylist[prov_id].c[city_id].n,
dist: null
};
}else{
var data = {
prov: city_json.citylist[prov_id].p,
city: city_json.citylist[prov_id].c[city_id].n,
dist: city_json.citylist[prov_id].c[city_id].a[dist_id].s
};
} changeHandle(data, areaType); // 返回两个处理好的数据
}
};
获取省市县数据data以及触发的change事件类型areaType
// 选择省份时发生事件
prov_obj.bind("change",function(){
cityStart();
selectChange('prov'); // 返回数据
}); // 选择市级时发生事件
city_obj.bind("change",function(){
distStart();
selectChange('city'); // 返回数据
}); // 选择区级时发生事件
dist_obj.bind("change",function(){
selectChange('dist'); // 返回数据
});
在各个事件中执行
前端使用
$("#s_city").citySelect({
prov: "江苏省",
city: "宿迁市",
dist: "宿城区",
nodata: "none"
},
function(data, type) {
selectAgent(data.city, data.dist);
});
使用回调回来的data数据,用于selectAgent函数中
function selectAgent(city,district){
$.ajax({
type:"POST",
url:"{sh::U('Index/ajax',array('todo'=>'getagent'))}",
data:"city="+city+"&district="+district,
success:function(json){
json = JSON.parse(json);
opt_str = "<option value=''>-请选择-</option>"
if(json.status == 1){
$.each(json.data,function(index,con){
opt_str += "<option value="+con.id+">"+con.name+" 电话:"+con.tel+"</option>"
})
}
$('#agent_id').html(opt_str);
}
});
}
去ajax获取相应的代理商数据。
改造插件完成。
jQuery改造插件,添加回调函数的更多相关文章
- jquery Ajax 不执行回调函数success的原因
jquery Ajax 不执行回调函数success的原因: $.ajax({ type: "post", contentType: "application/json& ...
- jQuery编写插件--封装全局函数的插件(一些常用的js验证表达式)
上一篇写到了jQuery插件的3种类型,介绍了第一种类型的写法--封装jQuery对象的方法插件.这一篇要介绍第二种插件类型:封装全局函数的插件:这类插件就是在jQuery命名空间内部添加函数:这类插 ...
- javascript与jQuery的each,map回调函数参数顺序问题
<script> var arr = [2,3,6,7,9]; //javascript中的forEach 和 map方法 arr.forEach(function(value,index ...
- jQuery hide() 参数callback回调函数执行问题
$("#b").click(function() { $("div").hide(1000,bbb); //-------------1 bbb是一个函数,但这 ...
- jQuery扩展插件和拓展函数的写法
<script type="text/JavaScript"> //jQuery插件的写法(需要传入操作对象) ;(function ...
- NGUI 添加回调函数
//缓动完成 TweenPosition tweenPos=GetComponent<TweenPosition>(); tweenPos.AddOnFinished(complete); ...
- jquery mutilselect 插件添加中英文自动补全
jquery mutilselect默认只能根据设置的option来进行自动提示 $.each(availableTags, function(key, value) { $('#channels') ...
- 给jquery-validation插件添加控件的验证回调方法
jquery-validation.js在前端验证中使用起来非常方便,提供的功能基本上能满足大部分验证需求,例如:1.内置了很多常用的验证方法:2.可以自定义错误显示信息:3.可以自定义错误显示位置: ...
- jQuery使用():Callbacks回调函数列表之异步编程(含源码分析)
Callbacks的基本功能回调函数缓存与调用 特定需求模式的Callbacks Callbacks的模拟源码 一.Callbacks的基本功能回调函数缓存与调用 Callbacks即回调函数集合,在 ...
随机推荐
- HttpRuntime类
HttpRuntime在ASP.NET处理请求中负责的是创建HttpContext对象以及调用HttpApplicationFactory创建HttpApplication. 其定义如下: publi ...
- javaWeb Cache技术――OSCache(转-全)
什么是osCache? 它是:http://baike.baidu.com/view/1835163.htm?fr=aladdin OSCache使用指南 一.下载安装 OSCache是一个基于web ...
- Oracle使用NLSSORT函数实现汉字的排序
1).按拼音首字母排序 SELECT * FROM T_TABLE ORDER BY NLSSORT(NAME, 'NLS_SORT=SCHINESE_PINYIN_M'); 2).按笔画排序SELE ...
- 【C语言】输入一组整数,求出这组数字子序列和中最大值
//输入一组整数.求出这组数字子序列和中最大值 #include <stdio.h> int MAxSum(int arr[],int len) { int maxsum = 0; int ...
- 使用lock_sga和pre_page_sga参数保证SGA常驻物理内存 .
Lock_sga LOCK_SGA locks the entire SGA into physical memory. It is usually advisable to lock the SGA ...
- java中23种设计模式
详情请看23种设计模式
- MP3/WAV 播放
一.编译libmad 1.先下载压缩包到本地,并解压 tar -xvzf libmad-0.15.1b.tar.gz -C ./ 2.进入源代码文件夹并配置 编写一个配置文件,便于< ...
- [Python笔记][第二章Python序列-tuple,dict,set]
2016/1/27学习内容 第二章 Python序列-tuple tuple创建的tips a_tuple=('a',),要这样创建,而不是a_tuple=('a'),后者是一个创建了一个字符 tup ...
- WPF实现窗体最小化后小图标在右边任务栏下
一 基本功能 1. 这里是用 NotifyIcon 控件来实现,但 WPF 下没有 NotifyIcon 控件,怎么办,用 WinForm 下的呗. 先引用 .NET 自带的两个程序集 Syste ...
- resolv.conf 是什么
From Wikipedia, the free encyclopedia This article does not cite any references or sources. Please h ...