pushState的增加新的PARAM,如何避免这种情况?

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

我有一个问题,我有一个pushState的标签:

history.pushState(null, null, '#/' + $(this).attr('href').substring(1));

而当我想改变这种pushState的有:

  function updateUrl() {
    history.pushState(null, null, `${location.href}/?page=` + this.page);
  }

当我点击分页按钮。我得到的链接是这样的:http://example.com/foo/bar/#revs/?page=1/?page=2/?page3

<button onclick="updateUrl(1)">1</button>
<button onclick="updateUrl(2)">2</button>

但我需要改变只PARAM page。我怎样才能解决这个问题?

javascript pushstate
1个回答
0
投票

使用location.href只是增加了目前链接。建立基于链接:

location.origin + location.pathname + location.hash.split('/')[0] + '/?page=' + this.page

了解更多关于location对象。

您可以随时在浏览器的控制台,以了解它的结构访问location对象。

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