2016-09-16 72 views
0

我有下面的代碼:使用Javascript - 在點擊處理忽略/跳過

$(document).on('click','#tab-personal',function(e){ 
loadit('personal');  
}); 

$("#tab-personal").click(function() 
{ 

}); 

,當我:

$(document).ready(function(){ 
     var rebuild = getParameterByName("rebuild"); 

     var createdStructures = $('#AtoZContentDiv').children().length; 


     if ((rebuild !== undefined && rebuild !== null && rebuild.indexOf("true") === 0) || (createdStructures === 0)) 
     { 
      // clean up pre-existing data 
      cleanUp(); 


      // create container structure 
      createFormLinkContainers(); 

      // Call SP web services to retrieve the information and create the A to Z 
      retrieveListData(); 

      completeInitialization(); 
     } 
     else 
     { 
      aggregateAll = jQuery.parseJSON($('#hdnAggregateAll').val()); 
      aggregatePersonal = jQuery.parseJSON($('#hdnAggregatePersonal').val()); 
      aggregateBusiness = jQuery.parseJSON($('#hdnAggregateBusiness').val()); 
      ministryAggregate = jQuery.parseJSON($('#hdnMinistryAggregate').val()); 
      caAggregate = jQuery.parseJSON($('#hdnCAAggregate').val()); 
      sTaxAggregate = jQuery.parseJSON($('#hdnSTaxAggregate').val()); 
      bTaxAggregate = jQuery.parseJSON($('#hdnBTaxAggregate').val()); 
      leTaxAggregate = jQuery.parseJSON($('#hdnLETaxAggregate').val()); 

      var type = getParameterByName("filter"); 

     $("#tab-all").click(function() 
     { 
       loadit('all'); 
     });   


     $("#tab-business").click(function() 
     { 
      loadit('business'); 
     }); 

     $("#tab-personal").click(function() 
     { 

     }); 


     $(document).on('click','#tab-personal',function(e){ 
     loadit('personal');  
     }); 


       buildFilterMenu(); 
       loadit('all'); 
     } 
    }); 

我一直在使用這兩種嘗試在他們中間放置一個斷點,沒有一個被擊中。

HTML:

<div id="tabs" style="display: inline-block;"> 
    <ul><li><a class="selected" id="tab-all" href="#" type="all"><b>All Forms</b></a></li> 
    <li><a id="tab-business" href="#" type="business"><b>Business</b> </a></li> 
    <li><a id="tab-personal" href="#" type="personal"><b>Personal</b> </a></li></ul> 
</div> 

全碼:http://pastebin.com/vzLPX3cU

這究竟是爲什麼?

+0

你嘗試執行添加聽衆在控制檯?試試這個:$(「#tab-personal」).on(「click」,function(){console.log(「click!」); }); – Sabik

+0

我在Chrome的開發人員工具調試器中放置了一個斷點。我曾嘗試把一個console.log('<<<<個人'),但代碼沒有被擊中。 – Brian

+0

與你的例子DOM作品https://jsfiddle.net/k06w4mos/。也許你有一些錯誤,你的代碼不能完全執行? – Sabik

回答

0
$(document).ready(function(){ 
    var rebuild = getParameterByName("rebuild"); 
    var createdStructures = $('#AtoZContentDiv').children().length; 
    if ((rebuild !== undefined && rebuild !== null && rebuild.indexOf("true") === 0) || (createdStructures === 0)){ 
     // clean up pre-existing data 
     cleanUp(); 

     // create container structure 
     createFormLinkContainers(); 

     // Call SP web services to retrieve the information and create the A to Z 
     retrieveListData(); 
     completeInitialization(); 
    }else{ 
     aggregateAll = jQuery.parseJSON($('#hdnAggregateAll').val()); 
     aggregatePersonal = jQuery.parseJSON($('#hdnAggregatePersonal').val()); 
     aggregateBusiness = jQuery.parseJSON($('#hdnAggregateBusiness').val()); 
     ministryAggregate = jQuery.parseJSON($('#hdnMinistryAggregate').val()); 
     caAggregate = jQuery.parseJSON($('#hdnCAAggregate').val()); 
     sTaxAggregate = jQuery.parseJSON($('#hdnSTaxAggregate').val()); 
     bTaxAggregate = jQuery.parseJSON($('#hdnBTaxAggregate').val()); 
     leTaxAggregate = jQuery.parseJSON($('#hdnLETaxAggregate').val()); 

     var type = getParameterByName("filter"); 
      buildFilterMenu(); 
      loadit('all'); 
    } 
    $("#tab-all").click(function(){ 
      loadit('all'); 
    });   

    $("#tab-business").click(function(){ 
     loadit('business'); 
    }); 

    $(document).on('click','#tab-personal',function(e){ 
     loadit('personal');  
    }); 
}); 
+0

單獨的$(document).ready()你的意思是?因爲代碼已經是這樣了。 – Brian

+0

是的,點擊事件應該在'$(document).ready()'函數內。並確保你已經包含Jquery庫。 – Samir

+0

我不理解您對代碼所做的更改。你能否詳細說明一下? – Brian

0

這是因爲您沒有禁用錨元素的默認行爲。在點擊事件停止默認行爲與event.preventDefault()

像這樣

$("#tab-personal").click(function(event){ 
    event.preventDefault(); # this will prevent the default behavior 
    # do something 
}); 

,因爲它已經是裏面ready()功能,你不必在一個多準備功能包裹它

+0

不幸的是,這並沒有工作 – Brian

+0

你使用的是jQuery選項卡嗎? – Bhadra

+0

不,使用HTML和CSS的自定義選項卡。 – Brian