javascript 如何从哈希数据中删除 # 符号?jQuery?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16539826/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 05:03:58  来源:igfitidea点击:

How do I remove # sign from hash data? jquery?

javascriptjquery

提问by MildlySerious

I need value of hash from url...

我需要来自 url 的哈希值...

var hash = window.location.hash;

So how do I get rid of #sign?

那么我如何摆脱#标志?

回答by MildlySerious

As easy as that.

就这么简单。

var hash = window.location.hash.substr(1)

There are also these two which return the exact same:

还有这两个返回完全相同的:

var hash = window.location.hash.slice(1)
var hash = window.location.hash.substring(1)

String.slice()was added to the spec a little later, although that's propably unimportant.

String.slice()稍后被添加到规范中,尽管这可能并不重要。

Using replace as mentioned below is an option, too.

使用下面提到的替换也是一种选择。

None of those options throw an error or warning if the window.location.hashstring is empty, so it really depends on your preferences what to use.

如果window.location.hash字符串为空,这些选项都不会抛出错误或警告,因此这实际上取决于您的偏好使用什么。

回答by Mohammad Adil

You can do this -

你可以这样做 -

hash = hash.replace(/^#/, '');

回答by flavian

Just cut out the first char:

只需切出第一个字符:

 var hash = window.location.hash.slice(1);

回答by Denys Séguret

You can simply do

你可以简单地做

 var hash = window.location.hash.slice(1);

Note that it doesn't produce an error if there is no hash in the location, it just returns "".

请注意,如果该位置没有散列,它不会产生错误,它只是返回"".

回答by Peter Schoep

window.location.href.substr(0, window.location.href.indexOf('#'))will do the trick

window.location.href.substr(0, window.location.href.indexOf('#'))会做的伎俩