Javascript history.replaceState未定义IE11

Javascript history.replaceState未定义IE11,javascript,jquery,twitter-bootstrap,twitter-bootstrap-3,Javascript,Jquery,Twitter Bootstrap,Twitter Bootstrap 3,我正在使用bootstrap的scrollspy更改页面滚动上突出显示的导航栏项目,然后使用history.replaceState将哈希添加到url。给了我那部分的想法。我还使用一些jquery在单击导航项时实现“平滑滚动”,在向下“平滑滚动”到锚点时关闭scrollspy,然后将哈希添加到url并重新打开scrollspy scrollspy部分工作正常,单击导航链接时出现问题。它在Chrome和Firefox中的效果与预期一样,但在IE10和IE11中,当点击链接时,它会在url的哈希之前

我正在使用bootstrap的scrollspy更改页面滚动上突出显示的导航栏项目,然后使用
history.replaceState
将哈希添加到url。给了我那部分的想法。我还使用一些jquery在单击导航项时实现“平滑滚动”,在向下“平滑滚动”到锚点时关闭scrollspy,然后将哈希添加到url并重新打开scrollspy

scrollspy部分工作正常,单击导航链接时出现问题。它在Chrome和Firefox中的效果与预期一样,但在IE10和IE11中,当点击链接时,它会在url的哈希之前添加
未定义的
,我不知道为什么

这是我的剧本:

//initiate scroll spy
$('body').scrollspy({ target: '.spy-active', offset: $offset + 1 });
$('.spy-active').on('activate.bs.scrollspy', function (e) {
    if (history.replaceState) {
    history.replaceState(null, "", $('a[href*=#]:not([href=#])', e.target).attr("href"));
    }
});

//smooth scroll
$('a[href*=#]:not([href=#])').click(function (e) {
    e.preventDefault();
    //deactivate scrollspy for duration of transition
    $('nav').removeClass('spy-active');

    //remove active class from all list items
    $('li').each(function () {
        $(this).removeClass('active');
    });
    //add active class to clicked item
    $(this).parent().addClass('active');
    var target = this.hash;
    var $target = $(target);
    $('html, body').stop().animate({
        'scrollTop': $target.offset().top - $offset
    }, 500, 'swing', function () {
        if (history.replaceState) {
            history.replaceState(null, "", target);
        }
        //reactivate scrollspy
        $('nav').addClass('spy-active');
    });
});

我的网站是

当你点击导航链接时,这一行在IE右键中执行:

history.replaceState(null, "", $('a[href*=#]:not([href=#])', e.target).attr("href"));
如果在
e.target
中未找到链接,则
.attr(“href”)
的结果为
未定义。因此,您需要检查是否找到链接,然后调用
history.replaceState

var matchLink = $('a[href*=#]:not([href=#])', e.target);
if(matchLink.length) {
    history.replaceState(null, "", matchLink.attr("href"));
}