【问题标题】:URL on address bar using jquery使用jquery的地址栏上的URL
【发布时间】:2018-12-11 15:35:33
【问题描述】:

我在 html 上使用这个脚本:

$(function () {
        $('table.menu a').bind('click', function (event) {

            var $anchor = $(this);

            //if you don't want to use the easing effects:
            $('html, body').stop().animate({
                scrollTop: $($anchor.attr('href')).offset().top - 170
            }, 1500);

            event.preventDefault();
        });
    });

但我有一个问题,地址栏上没有出现链接名称...

我的问题是:如何在地址栏上写我的锚点?

【问题讨论】:

  • 喜欢 url/#anchor 吗?删除 event.preventDefault();如果这是您想要的行为,则使 URL 指向 href。

标签: jquery html web


【解决方案1】:

我假设链接是页面上的哈希值。喜欢<a href="#div2">?在这种情况下,不需要此 JavaScript 代码。浏览器会自动滚动到具有该 ID 的元素。

如果您想(或出于任何原因需要)使用该代码,您可以设置location.hash,这将更新 URL 栏。

location.hash = $anchor.attr('href');

【讨论】:

    猜你喜欢
    • 2012-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-23
    • 1970-01-01
    • 2011-08-08
    相关资源
    最近更新 更多