1. 程式人生 > >選項卡切換後 重新整理頁面停留在當前選項卡頁面

選項卡切換後 重新整理頁面停留在當前選項卡頁面

– HTML

<div class="scroll_title" id="myTab1">
    <span class="ord" id="mytab_01" onclick="nTabs('mytab_01');location.href='#mytab_01'">所有訂單</span>
    <span class="ord" id="mytab_02" onclick="nTabs('mytab_02');location.href='#mytab_02'">待付款</span>
    <span class="ord" id="mytab_03" onclick="nTabs('mytab_03');location.href='#mytab_03'">已完成</span>
</div>
<div class="scroll_main">
    <div class="scroll_ps" id="mytab_01_1">
      	1111
    </div>
    <div class="scroll_ps" id="mytab_02_1">
        2222
    </div>
    <div class="scroll_ps" id="mytab_03_1">
       3333
    </div>
</div>

– JS

    var hash = location.hash;//獲取url引數
    if(hash){
        $('.scroll_ps').hide();
        $(hash).addClass('active');
        $(hash+'_1').show();
    }else {
        $('.scroll_ps').hide();
        $('#mytab_01').addClass('active');
        $('#mytab_01_1').show();
    }
    function nTabs(id) {
        $('.ord').removeClass('active');
        $('#'+id).addClass('active');
        $('.scroll_ps').hide();
        $('#'+id+'_1').show();
    }