我使用jQuery $.ajax创建网站,一旦我点击菜单图标,覆盖图打开并根据链接href属性显示请求的内容。当我这样做时,网站的网址会更改为您点击的任何链接。例如,如果您点击“about”,则URL将为http://example.com/#about。使用AJAX进行呼叫时不要更改URL
我不希望这样,因为当您关闭覆盖图时,网站保持为http://example.com/#about,并且如果您以这种方式进入网站,关于页面将不会打开,即使打开覆盖图也不会打开。我想知道是否有一种方法的网址不变,并始终是http://example.com。
这里是我当前的jQuery代码:
$(function() {
$('.w-container .w-nav-menu a').click(function() {
var $linkClicked = $(this).attr('href');
document.location.hash = $linkClicked;
var $pageRoot = $linkClicked.replace('#', '');
if (!$(this).hasClass("active")) {
$(".w-container .w-nav-menu a").removeClass("active");
$(this).addClass("active");
$.ajax({
type: "POST",
url: "load.php",
data: 'page='+$pageRoot,
dataType: "html",
success: function(msg){
if((msg))
{
$('.content').html(msg);
$('.content').hide().fadeIn();
}
}
});
}
else {
event.preventDefault();
}
});
var hash = window.location.hash;
hash = hash.replace(/^#/, '');
switch (hash) {
case 'products' :
$("#" + hash + "-link").trigger("click");
break;
case 'about' :
$("#" + hash + "-link").trigger("click");
break;
case 'storelocator' :
$("#" + hash + "-link").trigger("click");
break;
case 'media' :
$("#" + hash + "-link").trigger("click");
break;
case 'faq' :
$("#" + hash + "-link").trigger("click");
break;
case 'contact' :
$("#" + hash + "-link").trigger("click");
break;
}
});
2016-04-13
Josef