我从(URL)中删除了哈希,我找不到404页面

问题描述 投票:0回答:2

我为一个页面滚动网站创建了一个导航栏。一切正常。我将URL从:http://example.com#sectionb更改为http://example.com/sectionb

当我点击导航栏中的其他项目时,页面滚动到想要的部分,网址更改为http://example.com/sectionc

为此我使用:

id = $(this).attr('href');
var link = id.split('#')[1];
window.history.pushState("", "Title", link);

然而问题是,当我使用url http://example.com / sectionb刷新页面时,它显示页面未找到404!但是,当我使用http://example.com / #sectionb时,它会显示“sectionb”

我想要的是当我用http://example.com/sectionb刷新页面时,看到“sectionb”部分

HTML:

<nav>
  <ul>
    <li><a href="sectiona" data-uri="1">First</a></li>
    <li><a href="sectionb" data-uri="2">Second</a></li>
    <li><a href="sectionc" data-uri="3">Third</a></li>
  </ul>
</nav>

<div class="sections">
  <section id="sectiona"></section>
  <section id="sectionb"></section>
  <section id="sectionc"></section>
</div>

JS:

    var sections = $('section')
  , nav = $('nav')
  , nav_height = nav.outerHeight();

$(window).on('scroll', function () {
  var cur_pos = $(this).scrollTop();

  sections.each(function() {
    var top = $(this).offset().top - nav_height,
        bottom = top + $(this).outerHeight();
    if (cur_pos >= top && cur_pos <= bottom) {
      nav.find('a').removeClass('active');
      sections.removeClass('active');
      $(this).addClass('active');
      nav.find('a[href="#'+$(this).attr('id')+'"]').addClass('active');
    }
  });
});

nav.find('a').on('click', function (e) {
  var $el = $(this)
    , id = $el.attr('href');
  e.preventDefault();
  $('html, body').animate({
    scrollTop: $(id).offset().top - nav_height
  }, 500);

  var link = id.split('#')[1];
  window.history.pushState("", "Title", link);
  return false;
});
jquery html5 window.location
2个回答
2
投票

您的代码只会更新历史记录中的URL。这绝不意味着您放置的URL有效。

  • http://example.com/#sectionb表示在域根目录的默认文档中使用idsectionb定位元素并导航(滚动)到它。
  • http://example.com/sectionb表示在名为sectionb的子目录中加载默认文档

你似乎在说原始网址是:

http://example.com/#sectionb

您的代码将其更改为:

http://example.com/sectionb

然后,您希望该URL导航到:

http://example.com/#sectionb

这似乎是循环的。你想解决什么问题?


0
投票

问题解决了我编辑我的控制器:http://example.com/ {sect}

    /**
     * Matches /*
     *
     * @Route("/{sec}", name="home")
     * 
     */
    public function index($sec = null) 
    {
        //return new Response('Test');
        //dump($sec);
        return $this->render('pages/nav.html.twig', [
            'sec' => $sec,
        ]);
    }

谢谢

© www.soinside.com 2019 - 2024. All rights reserved.