我学习了“window.location.hash” new并尝试使用我的jquery代码而不是“window.location.href”,并且两者都给出了相同的结果。
代码在这里:
window.location.href = ($(e.currentTarget).attr("href"));
window.location.hash = ($(e.currentTarget).attr("href"));
他们之间有什么区别?
答案 0 :(得分:58)
对于http://[www.example.com]:80/search?q=devmo#test
哈希返回#符号后面的网址部分,包括#符号。 您可以侦听hashchange事件,以获得有关支持浏览器中哈希更改的通知。
Returns: #test
href 会返回整个网址。
Returns: http://[www.example.com]:80/search?q=devmo#test
答案 1 :(得分:9)
例如http://stackoverflow.com/#Page
href = http://stackoverflow.com/#Page
hash = #Page
答案 2 :(得分:3)
答案 3 :(得分:2)
hash
和href
都是window.location
对象的属性。 hash
是来自#
的网址的一部分(如果没有#
,则为空字符串),而href
是整个网址的字符串表示形式。
答案 4 :(得分:1)
hash属性返回URL的锚点部分,包括井号(#)。
答案 5 :(得分:0)
以下是window.location.href
和window.location.hash
对于网址http://www.manm.com/member/#!create
:
http://www.manam.com/member/#!create
#!create