jquery $ .ajax를 사용하여 웹 사이트를 개발하고있어 콘텐츠/디렉토리에있는 다른 페이지의 콘텐츠를로드합니다. 또한 해시 태그를 사용하여 뒤로 버튼, 다시로드 등을 활성화합니다.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);
}
});
메뉴 항목을 활성화하는 방법을 보려면 기본 메뉴 코드를 참조하십시오. – Nagarjun
추가했습니다. 코드 –
체크 아웃'history.pushState()' –