我正在使用jQuery$.ajax开发网站,以加载内容/目录中不同页面的内容,我还使用哈希标签激活返回按钮、重新加载等。 但是,我有个带有活动项的主菜单,是否有方法来记住活动主菜单项的状态?主菜单位于容器页上。谢谢!
$(function(){
$('.menu a')
.bind('click',function(e) {
e.preventDefault();
$('#content').html('Извините, страница не существует или находится в разработке!');
$('.menu a').each(function() {$(this).removeClass('activelink');});
location.hash=$(this).attr('href').match(/(([a-z]*)\W)*/)[2];
$(this).addClass('activelink');
return false;
});
$('#content').on('click','a',function(e) {
if (!($(this).hasClass('address') || $(this).hasClass('zoom'))){
e.preventDefault();
$('#content').html('Извините, страница не существует или находится в разработке!');
location.hash=$(this).attr('href').match(/(([a-z]*)\W)*/)[2];
return false;
}
else {
if ($(this).hasClass('address')) {
alert('Вы покидаете сайт The House Of Events. Возвращайтесь к нам еще!');
}
}
});
function hashChange(){
var page=location.hash.slice(1);
if (page!=""){
$.ajax({
type: "GET",
url: "content/" + page + ".html #sub-content",
success: function(data, status) {
$('#content').html(data);
},
error: function fail() {
$('#content').html('Error');
}
});
}
}
if ("onhashchange" in window){
$(window).on('hashchange',hashChange).trigger('hashchange');
} else {
var lastHash='';
setInterval(function(){
if (lastHash!=location.hash)
hashChange();
lastHash=location.hash;
},100);
}
});