欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

jQuery插件擴(kuò)展實(shí)例【添加回調(diào)函數(shù)】

 更新時(shí)間:2016年11月26日 14:42:48   作者:牛逼的霍嘯林  
這篇文章主要介紹了jQuery插件擴(kuò)展的方法,結(jié)合實(shí)例形式分析了jQuery插件添加回調(diào)函數(shù)與事件觸發(fā)機(jī)制的相關(guān)操作技巧,需要的朋友可以參考下

本文實(shí)例講述了jQuery插件擴(kuò)展的方法。分享給大家供大家參考,具體如下:

<script language="javascript" type="text/javascript">
function doSomething(callback) {
  // … 
  // Call the callback
  callback('stuff', 'goes', 'here'); // 給callback賦值,callback是個(gè)函數(shù)變量
}
function foo1(a, b, c) {
  // I'm the callback
  alert(a + " " + b + " " + c);
}
doSomething(foo1); // foo1函數(shù)將使用callback函數(shù)中的數(shù)據(jù) stuff goes here
var foo2 = function(a,b,c) {
  // I'm the callback
  alert(a + " " + b + " " + c);
}
doSomething(foo2); // foo2函數(shù)將使用callback函數(shù)中的數(shù)據(jù) stuff goes here
doSomething(function(a,b,c){
  alert(a + " " + b + " " + c); // function函數(shù)將使用callback函數(shù)中的數(shù)據(jù) stuff goes here
});
</script>

callback這個(gè)參數(shù)必須是函數(shù)才有效。才能起到回調(diào)的作用。

<script language="javascript" type="text/javascript">
function doSomething(callback) {
  // … 
  // Call the callback
  if(typeof callback === 'function'){
    callback('stuff', 'goes', 'here'); // 給callback賦值,callback是個(gè)函數(shù)變量
  }else{
    alert('jb51.net');
  }
}
function foo1(a, b, c) {
  // I'm the callback
  alert(a + " " + b + " " + c);
}
doSomething(foo1); // foo1函數(shù)將使用callback函數(shù)中的數(shù)據(jù) stuff goes here
var foo2 = function(a,b,c) {
  // I'm the callback
  alert(a + " " + b + " " + c);
}
doSomething(foo2); // foo2函數(shù)將使用callback函數(shù)中的數(shù)據(jù) stuff goes here
doSomething(function(a,b,c){
  alert(a + " " + b + " " + c); // function函數(shù)將使用callback函數(shù)中的數(shù)據(jù) stuff goes here
});
var foo3 = 'a';
doSomething(foo3);
</script>

foo3不是函數(shù)的時(shí)候,彈出jb51.net

jQuery實(shí)例

原函數(shù)

$.fn.citySelect=function(settings)

添加回調(diào)

$.fn.citySelect=function(settings, changeHandle) // 添加回調(diào)函數(shù)changeHandle

給回調(diào)函數(shù)賦值

//選項(xiàng)變動(dòng)賦值事件
var selectChange = function (areaType) { 
  if(typeof changeHandle === 'function'){ // 判斷callback是否是函數(shù)
    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); // 返回兩個(gè)處理好的數(shù)據(jù)
  }
};

獲取省市縣數(shù)據(jù)data以及觸發(fā)的change事件類型areaType

// 選擇省份時(shí)發(fā)生事件
prov_obj.bind("change",function(){
    cityStart();
    selectChange('prov'); // 返回?cái)?shù)據(jù)
});
// 選擇市級(jí)時(shí)發(fā)生事件
city_obj.bind("change",function(){
    distStart();
    selectChange('city'); // 返回?cái)?shù)據(jù)
});
// 選擇區(qū)級(jí)時(shí)發(fā)生事件
dist_obj.bind("change",function(){
    selectChange('dist'); // 返回?cái)?shù)據(jù)
});

在各個(gè)事件中執(zhí)行

前端使用

$("#s_city").citySelect({
  prov: "江蘇省",
  city: "宿遷市",
  dist: "宿城區(qū)",
  nodata: "none"
},
function(data, type) {
  selectAgent(data.city, data.dist);
});

使用回調(diào)回來(lái)的data數(shù)據(jù),用于selectAgent函數(shù)中

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=''>-請(qǐng)選擇-</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獲取相應(yīng)的代理商數(shù)據(jù)。

改造插件完成。

更多關(guān)于jQuery相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《jQuery常用插件及用法總結(jié)》、《jQuery擴(kuò)展技巧總結(jié)》、《jQuery切換特效與技巧總結(jié)》、《jQuery遍歷算法與技巧總結(jié)》、《jQuery常見經(jīng)典特效匯總》、《jQuery動(dòng)畫與特效用法總結(jié)》及《jquery選擇器用法總結(jié)

希望本文所述對(duì)大家jQuery程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論