-2

我需要使用#.

我正在使用location.split("#");,但它似乎不适用于 jQuery。

我想在 之后获取 URL 的最后一部分,#例如:

http://example.com/#/hello_world -> /hello_world

http://example.com/folder/dir/#test -> 测试

我怎样才能做到这一点?

4

3 回答 3

6

你真正想要location.hash的,它会返回你正在寻找的东西。

于 2013-06-12T03:17:50.637 回答
3

你需要使用

location.href.split('#')

location是一个对象而不是字符串,如果你想要完整的位置路径,那么你可以使用 location 的 hreg 属性

另一种解决方案是使用location.hashwhich 将在#

于 2013-06-12T03:15:43.030 回答
1

我想你正在寻找

location.hash.substr(1);

原因如下:

typeof location; //"object", .split won't work on objects
typeof location.hash; //"string", now it will works!

还要记住这与jQuery无关,否则...... http://i.stack.imgur.com/sGhaO.gif

于 2013-06-12T03:19:05.327 回答