Home > php教程 > PHP开发 > body text

jQuery plug-in extension example [add callback function]

高洛峰
Release: 2016-12-05 10:06:42
Original
1171 people have browsed it

本文实例讲述了jQuery插件扩展的方法。分享给大家供大家参考,具体如下:

<script language="javascript" type="text/javascript">
function doSomething(callback) {
  // … 
  // Call the callback
  callback(&#39;stuff&#39;, &#39;goes&#39;, &#39;here&#39;); // 给callback赋值,callback是个函数变量
}
function foo1(a, b, c) {
  // I&#39;m the callback
  alert(a + " " + b + " " + c);
}
doSomething(foo1); // foo1函数将使用callback函数中的数据 stuff goes here
var foo2 = function(a,b,c) {
  // I&#39;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>
Copy after login

callback这个参数必须是函数才有效。才能起到回调的作用。

<script language="javascript" type="text/javascript">
function doSomething(callback) {
  // … 
  // Call the callback
  if(typeof callback === &#39;function&#39;){
    callback(&#39;stuff&#39;, &#39;goes&#39;, &#39;here&#39;); // 给callback赋值,callback是个函数变量
  }else{
    alert(&#39;jb51.net&#39;);
  }
}
function foo1(a, b, c) {
  // I&#39;m the callback
  alert(a + " " + b + " " + c);
}
doSomething(foo1); // foo1函数将使用callback函数中的数据 stuff goes here
var foo2 = function(a,b,c) {
  // I&#39;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 = &#39;a&#39;;
doSomething(foo3);
</script>
Copy after login

foo3不是函数的时候,弹出jb51.net

jQuery实例

原函数

$.fn.citySelect=function(settings)
Copy after login

添加回调

$.fn.citySelect=function(settings, changeHandle) // 添加回调函数changeHandle
Copy after login

给回调函数赋值

//选项变动赋值事件
var selectChange = function (areaType) {
  if(typeof changeHandle === &#39;function&#39;){ // 判断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); // 返回两个处理好的数据
  }
};
Copy after login

获取省市县数据data以及触发的change事件类型areaType

// 选择省份时发生事件
prov_obj.bind("change",function(){
    cityStart();
    selectChange(&#39;prov&#39;); // 返回数据
});
// 选择市级时发生事件
city_obj.bind("change",function(){
    distStart();
    selectChange(&#39;city&#39;); // 返回数据
});
// 选择区级时发生事件
dist_obj.bind("change",function(){
    selectChange(&#39;dist&#39;); // 返回数据
});
Copy after login

在各个事件中执行

前端使用

$("#s_city").citySelect({
  prov: "江苏省",
  city: "宿迁市",
  dist: "宿城区",
  nodata: "none"
},
function(data, type) {
  selectAgent(data.city, data.dist);
});
Copy after login

使用回调回来的data数据,用于selectAgent函数中

function selectAgent(city,district){
    $.ajax({
      type:"POST",
      url:"{sh::U(&#39;Index/ajax&#39;,array(&#39;todo&#39;=>&#39;getagent&#39;))}",
      data:"city="+city+"&district="+district,
      success:function(json){
        json = JSON.parse(json);
        opt_str = "<option value=&#39;&#39;>-请选择-</option>"
        if(json.status == 1){
          $.each(json.data,function(index,con){
            opt_str += "<option value="+con.id+">"+con.name+" 电话:"+con.tel+"</option>"
          })
        }
        $(&#39;#agent_id&#39;).html(opt_str);
      }
    });
}
Copy after login

去ajax获取相应的代理商数据。

改造插件完成。


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!