本文实例讲述了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('jb51.net');
}
}
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不是函数的时候,弹出jb51.net
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获取相应的代理商数据。
改造插件完成。
