嘿,非常聪明的人!
我对此感到困惑,并尝试了一周,但找不到合乎逻辑的解决方案。
基本上,每当用户单击两组不同的链接时,我都希望附加准备好的 url。
到目前为止,在我的代码中发生了这种情况
$(selecter a.1).live('click', function(){
window.location.hash = '!/' + usr + '/' + $(this).attr('href').replace('.php', '/');
}
这将使网址看起来像:http ://www.site.com/!#/username/page1/
然后我调用一个函数来检测 url 中的哈希变化并将 url 返回到它应该是什么:
$(window).bind('hashchange', function(){
hash = window.location.hash;
//if( hash.indexOf( "/files/" ) == 6 )
//newHash = window.location.hash.substring(3).replace(usr + '/files', '').replace('/', '.php');
//else
newHash = window.location.hash.substring(3).replace(usr + '/', '').replace('/', '.php');
//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);
});
});
});
}
});
那里没有问题,问题是当用户单击第二组链接时,我想将其附加到 url 略有不同。
$(selecter a.2).live('click', function(){
window.location.hash = '!/' + usr + '/' + $(this).attr('href').replace('.php', '/');
}
看起来像这样:http ://www.site.com/!#/username/files/sub-page/
链接的 href 看起来像这样 href="files/sub-page.php"
我遇到的问题是,将窗口绑定到 hashchange 时,它将尝试将 url 准备回其原始“状态”,该状态仅适用于第一组链接。
我有一个想法,为什么不只是将原始 href 值替换为 window.location.hash 但后来意识到……如果用户重新加载页面怎么办?嗯嗯!原始哈希将丢失!并且 url 依赖于对哈希变化的检测,以便它可以加载正确的 php 页面。
好的,所以接下来我想也许我可以以某种方式切掉除了 url 中的最后一个单词之外的所有内容,并执行 replace('/', '.php') 以添加扩展名...
好的,另一个问题是在 url 中它指向 files/sub-page.php,它位于 files/ 目录中。
我最初将所有内容都放在一个目录中,并希望手动将 files/ 附加到 url,但运行时遇到相同逻辑的问题,所以我想我将它们移动到一个名为 files/ 的文件夹中,并弄清楚如何欺骗 js 离开那里的目录路径...
好吧,这就是我的故事……
我能得到一些帮助吗?