6

我使用这个脚本:

(function() {
    var tabContainers = $('div.tabs > div');
    tabContainers.hide().filter(':first').show();
    $(window).bind('hashchange', function () {
        var hash = window.location.hash || '#first';
        tabContainers.hide();
        tabContainers.filter(hash).show();
        $('div.tabs ul.tabNavigation a').removeClass('selected');
        $('a[href=' + hash +']').addClass('selected');
    });
    $(window).trigger("hashchange");
});

这部分代码给了我我需要的一切。例如,要从外部页面打开选项卡,我使用“index.php#first”href。它打开带有标签的页面,其中打开了确切的标签,但它跳转到锚点,我需要从它的顶部显示页面。

有谁知道,如何防止此代码的锚点跳转?

4

1 回答 1

2

Have you tried doing a return false inside the hashchange function?.

Otherwise, you can avoid using real IDs on your elements. That way it will not scroll.

Update: As you are using jQuery you can also try with: e.preventDefault. The function will need to receive e as parameter

于 2012-10-16T11:52:41.727 回答