【发布时间】:2011-06-29 07:07:04
【问题描述】:
嘿,非常聪明的人!
我对此感到困惑并尝试了一周,但找不到合乎逻辑的解决方案。
基本上,每当用户点击两组不同的链接时,我都希望附加一个准备好的网址。
到目前为止,在我的代码中发生这种情况
$(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/ 目录中的 files/sub-page.php。
我最初将所有内容都放在一个目录中,并想手动将 files/ 附加到 url,但运行时遇到了相同逻辑的问题,所以我想我将它们移动到一个名为 files/ 的文件夹中,并弄清楚如何欺骗js 将那个目录路径留在那里...
好吧,这就是我的故事......
我能得到一些帮助吗?
【问题讨论】:
标签: jquery hash window.location