因此,我有一个网站,我想要创建一个基于URL的哈希导航。我的目标是在不使用AJAX的情况下实现它,并且只根据特定的哈希更改显示设置。有什么方法可以用if
来做吗?如果我被开除了,我可能会嘲笑自己,但我尝试过这样的方法:
假设我的网址是东西网站/file.html#home
function someFunction () {
var hash = window.location.hash;
if (hash == home) {
//Changing some settings based on the hash
}
}
我会很感激你的帮助。
发布于 2018-11-01 13:59:03
window.location.hash
包含#
符号。可以将其包含在比较中,也可以将其从变量哈希中删除。
包括散列符号:
if (hash === "#home")
或删除散列符号:
var hash = "home"; // default
if (window.location.hash.length > 0) {
hash = window.location.hash.substr(1);
}
发布于 2018-11-01 14:02:01
使用indexOf()查看特定字符串是否在哈希字符串中。
if (hash.indexOf('mysubstring') > 0) { //do summat }
else if (hash.indexOf('nuther substring') > 0) { //do summat else }
(假设您继续使用哈希作为变量名)
https://stackoverflow.com/questions/53109798
复制相似问题