工具提示子菜单链接提供奇怪的 URL


Tooltip submenu links giving weird URLs

我有一个使用 jquery ajax 加载所有页面的网站。 基本上就像<a href="photos">photos</a>一样。然后 jquery 选取它并转到正确的文件夹并在末尾添加.php并将该文件加载到内容窗口中。然后它放置该 href 属性(在本例中为照片)并将 #current_pg_attr 放在 url 的末尾,因此如果内容窗口显示照片页面,则就像 www.mysite.com/#photos 一样。同样在我的一个主菜单中,我有一个子菜单,仅在悬停时显示,类似:

<ul id="main_nav"><br />
    <li><a href="home">home</a></li><br />
    <li><a href="photos">photos</a><br />
        <div id="menu2_submenu_tooltip"><a href="?com=viewall">view all stuff</a></div><br />
    </li><br />
</ul>

现在,单击子菜单时出现问题。就像如果我点击查看所有内容,它会带我去 www.mysite.com/#?com=viewall 而不是 www.mysite.com/?com=viewall#photos。我做错了什么?我认为这与在 url 中添加 jquery #somthing 的那行有关?此错误 url 不是问题,因为我可以通过获取 GET 变量并加载该页面来解决它,但我仍然想尝试正确处理这个问题。顺便说一下,这是我的ajax jquery代码的一部分:

var curr_page_hash = window.location.hash;
    $('#main_center').load('content/' + curr_page_hash.substr(1) + '.php');
    window.location.hash = curr_page_hash;
    $('#left_nav ul li a').click(function() {
    var page = $(this).attr('href');
    $('#main_center').html('<div id="load_img"><img src="template/img/loading.gif" alt="Loading..." /></div>');
    $('#main_center').load('content/' + page + '.php');
    window.location.hash = '#' + page; 
            return false;     
    });

对问题有什么帮助吗?

对于原始哈希,您可以执行以下操作:

// note I am not sure what window.location.hash returns if there is no hash but you should test that first to make this more efficient
var t = window.location;
var originalHash = "";
if(t.indexOf("#") > -1) {
   // grab the hash
   originalHash = window.location.hash;
}
因此,这

照顾了原始哈希,现在要设置新页面,您可以保持代码原样并在最后执行此操作

// don't need to add # because it is in originalHash already, it is safe to add originalHash because if there is no # in URL then it will be empty string
window.location.hash = page + originalHash; 

如果您尝试过此操作但不起作用,请确保编辑问题并发布最新代码