0

URL: www.domain.com/#ratings にアクセスすると、評価タブが開いているはずです。現在、URL に現在のタブが表示されていますが、タブのリンクには機能していません。

<script type="text/javascript">
    $(document).ready(function () {
        $('.tabs a').click(function () {
            switch_tabs($(this));
        });
        switch_tabs($('.defaulttab'));
    });

    function switch_tabs(obj) {
        $('.tab-content').hide();
        $('.tabs a').removeClass("selected");
        var id = obj.attr("rel");
        $('#' + id).show();
        obj.addClass("selected");
    }
</script>
<ul class="tabs">
    <li>
        <a href="#movies" class="defaulttab" rel="movies">Tutorial</a>
    </li>
    <li>
        <a href="#ratings" rel="ratings">Comments</a>
    </li>
</ul>
<div class="contentbox">
    <div class="tab-content" id="movies">movie tab</div>
    <div class="tab-content" id="ratings">rating tab</div>
</div>
4

3 に答える 3

1

これを試して、

 $(document).ready(function () {
        $('.tabs a').click(function () {
            var $this = $(this);
            $('.tabs a').removeClass("selected");
            $this.addClass('selected');
            switch_tabs($this.attr('href'));
            return false;
        });
        var anchor;
        var hash = $('a[href="' + location.hash + '"]');
        if(hash.length > 0){
            anchor = hash;
        }
        else{
            anchor = $('.defaulttab');
        }
        anchor.click();
    });

    function switch_tabs(hash) {
        $('.tab-content').hide(); 
        $(hash).show();   
        location.hash = hash;
    }
于 2012-06-05T07:03:27.443 に答える
0

switch_tabs(window.location.hash); を使用 ?

例えば:

    $(window).bind('hashchange', function() {
    alert(window.location.hash.split('#').pop());
        switch_tabs(window.location.hash);
    });

ハッシュ変更時に関数を呼び出す

于 2012-06-05T06:49:57.583 に答える