【问题标题】:jQuery go to hash without reloadjQuery无需重新加载即可进行哈希处理
【发布时间】:2017-05-11 13:11:16
【问题描述】:

我正在构建一个网页,当我浏览时不需要重新加载页面(我使用 ajax/php 来实现)。

当我浏览项目(很多)并打开其中一个时 - 我希望页面在我关闭页面时返回到页面的最后一个位置。

我使用锚来实现这一点。

我的代码:

$(document).on('click', '.content', function() {

  ... some other code ...


  $.ajax({
    type: 'POST',
    url: '/includes/content.php',
    data: info,
    dataType: 'json',
    success: function(result){

        if(type == 'close') {
            window.location.hash = '#' + hash;
            location.reload();
        }


    }
  });


return false;
});

问题是:jQuery 确实向 url 添加了一个哈希,但页面没有跳转到页面中的指定位置。 location.reload();确实这样做,但我不需要页面加载。

【问题讨论】:

  • 请提供minimal reproducible example。浏览器控制台是否抛出任何错误?
  • 缺失代码只是一堆变量集。
  • 应该 window.location.hash = '#hash' 实际跳转到锚点还是只是在 url 中设置锚点?

标签: jquery hash anchor


【解决方案1】:

如果你想滚动到指定的地方你可以动画html,body

$('html, body').animate({
   scrollTop: $("#myDiv").offset().top
}, 2000);

【讨论】:

    猜你喜欢
    • 2016-01-02
    • 2015-04-30
    • 1970-01-01
    • 2011-12-21
    • 1970-01-01
    • 2017-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多