我有这个网址 http://localhost:1234/pag1
当我点击 div 时,我想像下面这样:
http://localhost:1234/pag1?fare=standar
我有一个 onclick 事件函数:
<div id="" onclick="addUrl();"> </div>
以及js代码:
<script type="text/javascript">
function addUrl() {
var url = window.location.href;
window.location.hash = '?fare=standar';
};
</script>
这给了我这个结果:
http://localhost:1234/pag1#?fare=standar
而且我不知道如何删除 (#)。我只需要将参数添加到url,而不刷新页面。
建议我!谢谢。
最佳答案
使用 window.history.pushState( {} , '', '?fare=standar' );
这应该在不刷新的情况下更新 url。
您还可以对 pushState 做一些研究,因为实现可能不同。
关于javascript - 如何在不重新加载页面的情况下向我的网址添加参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33823422/