【问题标题】:disable url change when link to a different part of the same page is clicked单击指向同一页面不同部分的链接时禁用 url 更改
【发布时间】:2017-09-10 01:30:39
【问题描述】:

假设我在www.domain.com 上有一个按钮,通过锚标记<a href="#destination">text link</a> 链接到页面的下部

是否可以阻止浏览器url栏中的url显示www.domain.com/#destination

谢谢

【问题讨论】:

标签: javascript jquery html


【解决方案1】:

你可以用这个:

$("a.anchor").on("click", function(e) {      
    e.preventDefault();
    $(window).scrollTop($($(this).attr('href')).offset().top);
});
<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
<a href="#second" class="anchor">Second</a>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<h2 id="second">Second Part</h2>

现在锚链接在悬停时显示地址,但滚动到目的地而不是导航。

【讨论】:

    【解决方案2】:

    像这样使用jquery:

    $("#down").click(function () {
        $('html,body').animate({
            scrollTop: $("#b").offset().top
        });
    });
    
    $("#up").click(function ()
        $('html,body').animate({
            scrollTop: $("#a").offset().top
        });
    })
    

    希望这会有所帮助。

    小提琴:

    http://jsfiddle.net/ZGk5F/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-27
      • 1970-01-01
      • 2019-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-29
      • 1970-01-01
      相关资源
      最近更新 更多