嗯,在我的网站上有一个常见问题解答,用户可以通过 URL 访问特定问题,例如:
https://example.com/faq#question-1
为此,我使用 Jquery,这样:
if (window.location.hash) {
$(window.location.hash).open();
}
但是我希望如果用户在 URL 中输入多个哈希值,这样:
https://example.com/faq#question-1#question-2#question-3#question-n
除了第一个 #question-1
之外,所有哈希值都会从 URL 中删除
我该怎么做?
编辑:
如果用户输入 https://example.com/faq#question-1#question-2#question-3#question-n
则将网址更改为 https://example.com/faq#question-1
这样只有第一个哈希出现在 url 中,并且常见问题解答只需读取单个哈希。
最佳答案
它可以像这样简单
var hash = window.location.hash;
var filtered_hash = '';
if(hash.length > 0){
filtered_hash = '#' + hash.split("#")[1];
window.location.hash = filtered_hash;
}
console.log(filtered_hash);
关于javascript - 如何删除除第一个之外的所有 URL 哈希值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56101594/