我有一个使用 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;
如果您尝试过此操作但它不起作用,请确保您编辑问题并发布最新代码
关于php - 提供奇怪 URL 的工具提示子菜单链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11315249/