Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 片段URL转换_Javascript_Jquery_Jquery Bbq - Fatal编程技术网

Javascript 片段URL转换

Javascript 片段URL转换,javascript,jquery,jquery-bbq,Javascript,Jquery,Jquery Bbq,我已经有了烧烤插件,所以这只是一个表面的改变。我不知道如何将URL从/index.html#pages/index.html转换为/index.html#index 我想这基本上是一个片段转换,但我对JS非常陌生。有什么想法吗 您只需使用window.location.hash就可以访问哈希,它会返回一个字符串,因此您可以对其执行任何您喜欢的字符串操作。例如: var temp = window.location.hash; // == #pages/index.html temp = temp

我已经有了烧烤插件,所以这只是一个表面的改变。我不知道如何将URL从
/index.html#pages/index.html
转换为
/index.html#index


我想这基本上是一个片段转换,但我对JS非常陌生。有什么想法吗

您只需使用window.location.hash就可以访问哈希,它会返回一个字符串,因此您可以对其执行任何您喜欢的字符串操作。例如:

var temp = window.location.hash; // == #pages/index.html
temp = temp.replace("pages/","").replace(".html","");
window.location.hash = temp; //sets the hash to #index

@您是否设置了
hashchange
事件?您可以。这只是操作散列的代码。你的问题中没有提到变化?如果是,你应该解释你想如何使用它。我为您找到了一些关于onhashchange的信息,一旦我知道您的期望,我就可以更改我的答案,将其包括在内。看起来jquery有一个很好的onhashchange事件。这里有一些信息-