我学习了 "window.location.hash",并在我的 jQuery 代码中尝试使用它,而不是 "window.location.href",结果两者都产生了相同的结果。
代码在这里:
window.location.href = ($(e.currentTarget).attr("href"));
window.location.hash = ($(e.currentTarget).attr("href"));
它们之间有什么区别?
我学习了 "window.location.hash",并在我的 jQuery 代码中尝试使用它,而不是 "window.location.href",结果两者都产生了相同的结果。
代码在这里:
window.location.href = ($(e.currentTarget).attr("href"));
window.location.hash = ($(e.currentTarget).attr("href"));
它们之间有什么区别?
对于像 http://[www.example.com]:80/search?q=devmo#test
这样的URL:
hash
- 返回URL中跟在 #
符号后面的部分,包括 #
符号。您可以在支持的浏览器中监听 hashchange
事件,以便在哈希值发生更改时得到通知。
Returns: #test
href
- 返回完整的 URL。
Returns: http://[www.example.com]:80/search?q=devmo#test
例如,在 http://stackoverflow.com/#Page
上进行测试。
href = http://stackoverflow.com/#Page
hash = #Page
window.location.href
和window.location.hash
的简单例子:http://www.manm.com/member/#!create
:http://www.manam.com/member/#!create
- hash: #!create
hash
和href
是window.location
对象的两个属性。 hash
是URL中从#
开始(如果没有#
则为空字符串)的部分,而href
是整个URL的字符串表示。
#
符号。 - Amir Raminfarhash属性返回URL的锚点部分,包括#号。
var hash = (location.href.split("#")[1] || "");
- naXa stands with Ukrainevar hash = location.hash.substring(1);
的意思是获取当前 URL 中的哈希值,而不包括 "#" 符号。 - lowtechsun