如何使用JS隐藏URL中的#哈希 [英] How to hide # hash in a url using js
问题描述
我有一个滚动插件,该插件使用div ID滚动到特定锚点,
I have a scroll plugin that uses my div ID to scroll to a specific anchor,
它正在创建如下网址:http://example.com/#examplediv|700
It's creating urls like this: http://example.com/#examplediv|700
我想找到一种使用js或其他建议的方法来隐藏网址中的哈希值的方法
I want to find a way using js or any other suggested method to hide the hash in the url
我想将此:http://example.com/#examplediv|700
转换为:http://example.com/
有什么想法吗?
推荐答案
您可以修改正在使用的滚动插件,也可以将其自己添加到侧面,但是您需要执行以下操作:
You can either modify the scroll plugin you are using or add it yourself on the side but you will want to do something like this:
假设:您担心与此滚动有关的所有DIV都必须具有anchor-scrolls
类.
Assumption: All DIVs that you are concerned with regarding this scrolling will need to have the anchor-scrolls
class.
HTML
<a href="#anchor-hash" class="anchor-scrolls">foo</a>
JS
//using jQuery
(function($){
$('.anchor-scrolls').on('click', function(evt){
evt.preventDefault(); //prevents hash from being append to the url
});
)(window.jQuery);
这篇关于如何使用JS隐藏URL中的#哈希的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!