我有一个小脚本可以从网址中删除哈希值,但是它导致了一个问题。因为我正在影响历史记录,所以它不允许用户使用浏览器中的后退按钮返回到他们所在的上一页。有可能有更干净的方法来做到这一点吗?
//Tab links in navbar:
$(document).ready(function() {
function navHash() {
var hash = document.location.hash;
if (hash) {
history.pushState('', document.title, window.location.pathname);
$('.nav-stacked a[href="'+hash+'"]').tab('show');
}
}
navHash();
$(window).on('hashchange', navHash)
});
最佳答案
以防万一其他人偶然发现这个问题,这里是更正后的代码。
//Tab links in navbar:
$(document).ready(function() {
function navHash() {
var hash = document.location.hash;
if (hash) {
history.replaceState('', document.title, window.location.pathname);
$('.nav-stacked a[href="'+hash+'"]').tab('show');
}
}
navHash();
$(window).on('hashchange', navHash)
});
关于javascript - 从 URL 中删除哈希值而不是使用 PushState 的更好方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45615250/