为什么我的javascript阻止浏览器访问链接

为什么我的javascript阻止浏览器访问链接,javascript,jquery,Javascript,Jquery,我有一个脚本,可以在每个导航元素位于文档中的相应部分时向其添加一个“active”类。该脚本改编自W3愚人网站,因此在编写时没有考虑我的场景 不同之处在于,在W3傻瓜网站上,导航链接仅指页面上的元素,而在我的导航中,末尾有一个元素指的是新页面 问题是,正如我所改编的那样,它适用于引用页面上各部分的链接。但是,由于我不知道的原因(抱歉-JS/jQuery新手),它阻止浏览器跟随新页面的链接(博客链接) 我试着通读代码并理解脚本在做什么——但是我似乎无法理解为什么它阻止单击外部链接,或者更具体地说,

我有一个脚本,可以在每个导航元素位于文档中的相应部分时向其添加一个“active”类。该脚本改编自W3愚人网站,因此在编写时没有考虑我的场景

不同之处在于,在W3傻瓜网站上,导航链接仅指页面上的元素,而在我的导航中,末尾有一个元素指的是新页面

问题是,正如我所改编的那样,它适用于引用页面上各部分的链接。但是,由于我不知道的原因(抱歉-JS/jQuery新手),它阻止浏览器跟随新页面的链接(博客链接)

我试着通读代码并理解脚本在做什么——但是我似乎无法理解为什么它阻止单击外部链接,或者更具体地说,如何修复它

有人能提出最简单的方法来解决我的问题,而不破坏脚本的原始功能吗

在这里观看现场直播:

或者

标记:

<div id="site-nav">

<div class="wrap">

    <nav id="nav-links">

            <a href="#jag-home" class="section-link scroll top">Top</a>

            <a href="#background-section" class="section-link scroll skills">Background</a>

            <a href="#projects-section" class="section-link scroll projects">Projects</a>

            <a href="#random-section" class="section-link scroll random">Random</a>

            <a href="#site-footer" class="section-link scroll credits">Credits</a>

            <a href="http://blog.joelglovier.com" class="section-link blog" title="my tumblr">Blog <span class="icon"></span></a>

    </nav>

</div>
(function($) {
  $(function() {

    var $nav = $('#nav-links'),
        $navLinks = $nav.find('a.scroll'),
        cache = {};
        $docEl = $( document.documentElement ),
        $body = $( document.body ),
        $window = $( window ),
        $scrollable = $body; // default scrollable thingy, which'll be body or docEl (html) 

    // find out what the hell to scroll ( html or body )
    // its like we can already tell - spooky
    if ( $docEl.scrollTop() ) {
        $scrollable = $docEl;
    } else {
        var bodyST = $body.scrollTop();
        // if scrolling the body doesn't do anything
        if ( $body.scrollTop( bodyST + 1 ).scrollTop() == bodyST) {
            $scrollable = $docEl;
        } else {
            // we actually scrolled, so, er, undo it
            $body.scrollTop( bodyST - 1 );
        }
    }       

    // build cache
    $navLinks.each(function(i,v) {
        var href =  $( this ).attr( 'href' ),
            $target = $( href );
        if ( $target.length ) {
            cache[ this.href ] = { link: $(v), target: $target };
        }
    });

    // handle nav links
    $nav.delegate( 'a', 'click', function(e) {
    //  alert( $scrollable.scrollTop() );
        e.preventDefault(); // if you expected return false, *sigh*
        if ( cache[ this.href ] && cache[ this.href ].target ) {
            $scrollable.animate( { scrollTop: cache[ this.href ].target.position().top }, 600, 'swing' );
        }
    });

    // auto highlight nav links depending on doc position
    var deferred = false,
        timeout = false, // so gonna clear this later, you have NO idea
        last = false, // makes sure the previous link gets un-activated
        check = function() {
            var scroll = $scrollable.scrollTop(),
                height = $body.height(),
                tolerance = $window.height() * ( scroll / height );

            $.each( cache, function( i, v ) {
                // if we're past the link's section, activate it
                if ( scroll + tolerance >  v.target.position().top  ) {
                    last && last.removeClass('active');
                    last = v.link.addClass('active');
                } else {
                    v.link.removeClass('active');
                    return false; // get outta this $.each
                }
            });

            // all done
            clearTimeout( timeout );
            deferred = false;
        };

    // work on scroll, but debounced
    var $document = $(document).scroll( function() {
        // timeout hasn't been created yet
        if ( !deferred ) {
            timeout = setTimeout( check , 250 ); // defer this stuff
            deferred = true;
        }
    });

    // fix any possible failed scroll events and fix the nav automatically
    (function() {
        $document.scroll();
        setTimeout( arguments.callee, 1500 );
    })(); 

});

})(jQuery)

您正试图告诉它滚动到“http://...“它在当前页面上不存在,因此它失败并且什么也不做

如果您将代码更改为此,它应该可以工作

// handle nav links
$nav.delegate( 'a', 'click', function(e) {
//  alert( $scrollable.scrollTop() );
    e.preventDefault(); // if you expected return false, *sigh*
    if ( cache[ this.href ] && cache[ this.href ].target ) {
        // preserve http:// links
        if (this.href.substr(0, "http://".length) == 'http://')
            location.href = this.href;
        else
            $scrollable.animate( { scrollTop: cache[ this.href ].target.position().top }, 600, 'swing' );
    }
});

您试图告诉它滚动到“http://...“它在当前页面上不存在,因此它失败并且什么也不做

如果您将代码更改为此,它应该可以工作

// handle nav links
$nav.delegate( 'a', 'click', function(e) {
//  alert( $scrollable.scrollTop() );
    e.preventDefault(); // if you expected return false, *sigh*
    if ( cache[ this.href ] && cache[ this.href ].target ) {
        // preserve http:// links
        if (this.href.substr(0, "http://".length) == 'http://')
            location.href = this.href;
        else
            $scrollable.animate( { scrollTop: cache[ this.href ].target.position().top }, 600, 'swing' );
    }
});

顺便说一句,我真的很喜欢那个网站。非常非常圆滑:)顺便说一句,我真的很喜欢那个网站。非常非常圆滑:)该死——还是不行。我按照你的建议修改并上传了直播,但没有骰子。哦-但我刚刚将$nav.delegate('a',…改为'a.scroll',成功了!该死-仍然不起作用。我按照你的建议修改并上传了直播,但没有骰子。哦-但我刚刚将$nav.delegate('a',…改为'a.scroll',成功了!