jQuery - 链接到ul.tabs之外的选项卡

3

我该如何修改下面的脚本,以便在 ul.tabs 外部创建可点击到不同标签页的链接?

非常感谢您的帮助。

<script type="text/javascript">
    $(document).ready(function() {
        var $tabContent = $(".tab-content"),
            $tabs = $("ul.tabs li"),
            tabId;

        $tabContent.hide();
        $("ul.tabs li:first").addClass("active").show();
        $tabContent.first().show();

        $tabs.click(function() {
            var $this = $(this);
            $tabs.removeClass("active");
            $this.addClass("active");
            $tabContent.hide();
            var activeTab = $this.find("a").attr("href");
            $(activeTab).fadeIn();
            //return false;
        });

        // Grab the ID of the .tab-content that the hash is referring to
        tabId = $(window.location.hash).closest('.tab-content').attr('id');

        // Find the anchor element to "click", and click it
        $tabs.find('a[href=#' + tabId + ']').click();
    })

    $('a').not('.tabs li a').on('click', function(evt) {
        evt.preventDefault();
        var whereTo = $(this).attr('goto');
        $tabs = $("ul.tabs li");
        $tabs.find('a[href=#' + whereTo + ']').trigger('click');
        //alert(attr('name'));
    //alert( $('#'+whereTo+' a').offset().top );
        $('html, body').animate({
            scrollTop: $('#'+whereTo+' a').offset().top
        });

    });

    $(function() {
        $('a.refresh').live("click", function() {
            location.reload();
        });
    });
</script>

代码来源: http://jsfiddle.net/dhirajbodicherla/TGMDd/2/

(本文是关于IT技术的翻译,下同)
1个回答

0

已经为您准备好了,只需指定与要转到的选项卡的 href 匹配的 goto 属性即可:

<a href="#" goto="my_tab">Clicky</a>

将前往设置为选项卡的标签页

<ul class="tabs">
    <li><a href="#my_tab">It goes here</a></li>

我不太认同 :) http://jsfiddle.net/TGMDd/7/ 请看页面顶部的链接 - Joe
没事了,经过一夜的睡眠,我按照你的jsfiddle做法已经解决了问题,非常感谢Joe! - TommyTheCat
@Joe - 有没有可能防止页面跳转到内容的顶部?还有,需要链接哪些库?我在jsfiddle上已经实现了,但在我的实际网站上却没有。我只链接了jq 1.10.1。谢谢! - sloga
@sloga,你最好提出一个新的问题,并提供所有细节 - 我不太确定你所说的“跳到内容顶部”是什么意思,如果没有代码可查看,我只能猜测。 - Joe

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接