时间:2011-07-26 10:03:30

标签: jquery location

说我有一个如下所示的URL ...如何激活正确的标签?

http://domain.com/safety.php#tabOne

继承我的HTML:

<div id="tabsWrapper">
              <div class="tabMenu">
                <ul class="tabset">
                  <li><a class="tab active" href="#tabOne">Safety First</a></li>
                  <li><a class="tab" href="#tabTwo">BS8848 &amp; LoTC</a></li>
                  <li><a class="tab" href="#tabThree">Know Before You Go</a></li>
                </ul>
              </div>
              <div id="tabbedContent">
                  <section class="contentTab" id="tabOne" style="display: block;">Content here</section>
                  <section class="contentTab" id="tabTwo">
                    <h3 style=""></h3>
                     </section>
                  <section class="contentTab" id="tabThree">
                    <h3 style=""></h3>
                     </section>
              </div>
              <div class="clear"></div>

            </div>

和我的观察:

$('.tabset>li>a').click(function(){
    var $tab;
    $(this).closest('.tabset').find('>li>a.active').removeClass('active');
    $(this).addClass('active');
    $tab = $($(this).attr('href'));
    $tab.siblings().hide();
    $tab.find('>div').show();
    $tab.fadeIn();
    return false;
});
$('#tabbedContent').each(function(){
    $(this).find(':first-child').fadeIn();
});

2 个答案:

答案 0 :(得分:3)

if(location.hash) {
        $('#tabbedContent').each(function(){
            $(this).find("section#" + location.hash.substr(1)).fadeIn();
        });
    } else {
        $('#tabbedContent').each(function(){
            $(this).find(':first-child').fadeIn();
        });
    }

答案 1 :(得分:0)

使用location.hash :)!您可能需要先删除#