Javascript jQuery滚动:防止锚文本出现在url中

Javascript jQuery滚动:防止锚文本出现在url中,javascript,jquery,jquery-click-event,Javascript,Jquery,Jquery Click Event,因此,我的页面上的jQuery滚动运行良好。 但我想改变一件事。 我不想在url中显示锚文本。即#产品或#联系信息 HTML代码: <div> <a href="#products">Products</a> <a href="#contactinfo">Contact info</a> </div> <div id="products"></div> <div id="contactinf

因此,我的页面上的jQuery滚动运行良好。 但我想改变一件事。 我不想在url中显示锚文本。即#产品或#联系信息

HTML代码:

<div>
<a href="#products">Products</a>
<a href="#contactinfo">Contact info</a>
</div>

<div id="products"></div>

<div id="contactinfo"></div>
    $(document).ready(function(){
    $('a[href^="#"]').on('click',function (e) {
        e.preventDefault();

        var target = this.hash,
        $target = $(target);

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

只需添加一个
返回false
的末尾,单击
功能。 编辑:我的意思是,很可能删除这一行。。这正是你不想要的?表面上

window.location.hash = target;

只需添加一个
返回false
的末尾,单击
功能。 编辑:我的意思是,很可能删除这一行。。这正是你不想要的?表面上

window.location.hash = target;
试试这个:

HTML:

试试这个:

HTML:


好久不见,但您是否尝试将其更改为$('a[href^=“#”]”)好久不见,但您是否尝试将其更改为$('a[href^=“#”]”)好久不见?对于我来说,它可以工作,但无法测试哈希url,因为它是一个iFrame,对于我来说,添加返回值false不会更改任何内容;并删除window.location.hash=target@Alex Haha,实际上只是注意到我的不起作用,因为jQuery没有被使用:对于我来说,它起作用了,不能测试哈希url,但是因为它是一个iFrame,对于我来说,添加返回false不会改变任何东西;并删除window.location.hash=target@Alex Haha,实际上刚刚注意到我的不起作用,因为jQuery没有被使用:D
$(document).ready(function(){
    $('.link').on('click',function (e) {
        $('html, body').stop().animate({
            'scrollTop': $($(this).attr('rel')).offset().top
        }, 900, 'swing', function () {});
    });
});