Javascript 如何基于链接单击动态更改url上的哈希位置
我只处理了一个链接实例,但我想合并我的代码,并对每个链接实例重复使用相同的代码段 目前我的工作:Javascript 如何基于链接单击动态更改url上的哈希位置,javascript,hash,Javascript,Hash,我只处理了一个链接实例,但我想合并我的代码,并对每个链接实例重复使用相同的代码段 目前我的工作: $("nav a").live('click', function(){ window.location.hash = '!/' + usr + '/' + $(this).attr("href").replace('.php', '/'); origHash = $(this).attr("href"); return false; });
$("nav a").live('click', function(){
window.location.hash = '!/' + usr + '/' + $(this).attr("href").replace('.php', '/');
origHash = $(this).attr("href");
return false;
});
$("#files-left-pane a").live('click', function(){
window.location.hash = '!/' + usr + '/files/' + $(this).attr("href").replace('.php', '/');
origHash = $(this).attr("href");
return false;
});
$(window).bind('hashchange', function(){
dump = window.location.hash;
newHash = window.location.hash.substring(3).replace(usr + '/', '').replace('/', '.php');//.replace('files/', '');
//newHash = window.location.hash.replace(dump, origHash, 'g');
console.log(newHash);
if (newHash) {
$wrap.find("#main-content").fadeOut(200, function() {
$wrap.load(newHash + " #main-content", function() {
$content.fadeIn(function() {
$wrap.animate({
height: baseHeight + $content.height() + "px"
}, 500);
});
});
});
}
});
现在,如果用户单击$(“NAVA”),它将使window.location.hash
如下所示
(在本例中,用户单击
)
www.site.com/#/s2xi/文件/
$(window).bind('hashchange')然后将哈希转换为
www.site.com/files.php
然后,如果用户单击位于files.php中的子菜单中的$(“#files left pane a”)。window.location.hash将如下所示:
(在本例中,用户单击
)
www.site.com/#/s2xi/文件/购买/
$(window).bind('hashchange')随后应将哈希转换为
www.site.com/buy.php如果你只需要最后一个字并添加“.php”,那很容易 你有两种方法,我认为最简单的一种是分割(另一种是正则表达式) 应该很容易做到这一点 顺便说一下,正则表达式的版本是:
newHash = window.location.hash.match(/[^\/]*\/$/)[0].replace("/","") + ".php"
对,但我也想删除#/user/files/来自第二次单击事件的url,并将.php添加到页面名称。我很难理解您想要做什么。
newHash = window.location.hash.match(/[^\/]*\/$/)[0].replace("/","") + ".php"