隐藏网址中的片段标识符

时间:2014-07-15 10:56:37

标签: javascript jquery

我需要在同一页面点击div后隐藏网址中的片段标识符。

这是我的js代码:

$(document).ready(function(){

$('a[href^="#"]').on('click',function (e) {
    e.preventDefault();

    var target = this.hash,
    $target = $(target);
    alert(target);
    $('html, body').stop().animate({
        'scrollTop': $target.offset().top
    }, 900, 'swing', function () {
        window.location.hash = target;
    });
 });

});

网址就像这样http://www.example.com#about,但我希望像这样http://www.example.com

我尝试在点击功能结束时更改$target.offsetreturn false;,但它们无效。

1 个答案:

答案 0 :(得分:0)

尝试删除动画完成回调。
由于你做preventDefault()我很确定如果删除它就不会再使用哈希值了。

如果这不起作用,请将其替换为window.location.hash = ''

相关问题