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即回调函数集合,在 ...
随机推荐
- 如何在WPF程序中使用ArcGIS Engine的控件
原文 http://www.gisall.com/html/47/122747-4038.html WPF(Windows Presentation Foundation)是美国微软公司推出.NET ...
- Apache、php、mysql单独安装配置
php, 安装版的,http://www.php.net/manual/zh/install.php.也有不安装版直接配置的. 在Windows 7下如何进行PHP配置环境. PHP环境在Window ...
- Working——流程关系状态表
--主表单 select * from ce_administration_procure t where t.id ='HZe992733d668dc6013d671df4760349'; --流程 ...
- 安装开源项目 MultiType (基于 RecyclerView)出现的各种问题 -- 自己的第一篇博客
一.引入开源项目的方式 使用开源项目 MultiType 的两种方式: 1.maven引入:在主Module 的 build.gradle 中加入 dependencies { ...... comp ...
- hdu 1502 Regular Words_高精度+dp
题意:问按规则排成的串有多少个A(c)>= B(c) >= C(c) 思路:因为写大整数太累,就偷懒了一下直接用java水过 import java.math.BigInteger; im ...
- jQuery.extend()、jQuery.fn.extend()扩展方法示例详解
jQuery自定义了jQuery.extend()和jQuery.fn.extend()方法.其中jQuery.extend()方法能够创建全局函数或者选择器,而jQuery.fn.extend()方 ...
- Ajax跨域访问解决办法
方法1. jsonp实现ajax跨域访问示例 jsp代码: <body> <input type="button" onclick="testJsonp ...
- Comet学习资料
什么是Comet: http://baike.baidu.com/view/577938.htm?fr=ala0_1 Comet介绍: http://www.ibm.com/developerwork ...
- 评教数据整理专用VBA小程序
这次评教的所有数据存放在两个数据库中,比如说给某教师评论的学生有100个,可是结果有40个的数据在数据库A中,另外60人的数据在数据库B中.那么,如何将两个库中的数据整合,最后得到教师的准确成绩成为了 ...
- Maven 工程下 Spring MVC 站点配置 (三) C3P0连接池与@Autowired的应用
Maven 工程下 Spring MVC 站点配置 (一) Maven 工程下 Spring MVC 站点配置 (二) Mybatis数据操作 前两篇文章主要是对站点和数据库操作配置进行了演示,如果单 ...