2012-08-07 62 views
1

我已经创建了jquery选项卡。基于查询字符串的活动选项卡

<div id="tabs_container"> 
     <ul id="tabs"> 
      <li class="active"><a href="#tab1">Allgemeines</a></li> 
      <li><a href="#tab2">Kontakt</a></li> 
      <li><a href="#tab3">Musik</a></li> 
      <li><a href="#tab4">Hauptfoto</a></li> 
      <li><a href="#tab5">Einstellungen</a></li> 
     </ul> 
    </div> 
    <div id="tabs_content_container"> 
     <div id="tab1" class="tab_content" style="display: block;"> 
      <?php include('1.php'); ?> 
     </div> 
     <div id="tab2" class="tab_content"> 
      <?php include('2.php'); ?> 
     </div> 
     <div id="tab3" class="tab_content"> 
      <?php include('3.php'); ?> 
     </div> 
     <div id="tab4" class="tab_content"> 
      <?php include('4.php'); ?> 
     </div> 
     <div id="tab5" class="tab_content"> 
      <?php include('5.php'); ?> 
     </div> 
    </div> 
</div> 
<script type="text/javascript"> 
/* <![CDATA[ */ 
$(document).ready(function(){ 
    $("#tabs li").click(function() { 
     // First remove class "active" from currently active tab 
     $("#tabs li").removeClass('active'); 

     // Now add class "active" to the selected/clicked tab 
     $(this).addClass("active"); 

     // Hide all tab content 
     $(".tab_content").hide(); 

     // Here we get the href value of the selected tab 
     var selected_tab = $(this).find("a").attr("href"); 

     // Show the selected tab content 
     $(selected_tab).fadeIn(); 

     // At the end, we add return false so that the click on the link is not executed 
     return false; 
    }); 
}); 
/* ]]> */ 
</script> 

我需要从URL选择的选项卡,这意味着如果http://example.com/settings.php#tab2

片2一个有源标签。 #tab3然后选项卡3一个活动选项卡。我怎样才能做到这一点?

回答

1

window.onhashchange

使用上述本地代码。它会在网址发生散列变化时触发

window.onhashchange=function(){ 
if(document.location.hash ==="#tab2"){ 
//write your code here 
} 


} 

希望它能帮上忙。

5
$(document).ready(function(){ 
    $(".active").removeClass("active"); 
    $(document.location.hash).addClass("active"); 
}); 
相关问题