所以,我有一个函数,起初应该可以使用我的导航栏。我点击了某个链接,然后在#content
div中通过AJAX加载页面。但是如果我在div中有一个链接怎么办?
我的意思是,AJAX调用一个页面,其中包含一个应该与AJAX一起使用的链接,但只有在我直接访问页面时才会起作用(如果#content
div中的内容未被AJAX调用),否则它就像普通的href
链接一样运行。
我的AJAX功能:
$(function() {
$('a').click(function(e) {
$("#loading").show();
$("#content").animate({ opacity: 0 });
var href = $(this).attr("href");
ajaxtitle = $(this).attr("ajaxtitle");
//this.a = ajaxtitle;
loadContent(href);
// HISTORY.PUSHSTATE
window.history.pushState('', ajaxtitle, href);
document.title = ajaxtitle;
e.preventDefault();
});
//MAKE BACK/FORWARD WORKS
window.onpopstate = function(event) {
$("#loading").show();
//console.log("pathname: "+location.pathname);
loadContent(location.pathname);
};
})
//AJAX ITSELF
function loadContent(url){
$.ajax({
url: url,
type: 'GET',
error: function(){
alert("O");
},
success:
function(data){
$('#content').html(data);
$("#loading").hide();
$("#content").animate({ opacity: 1 })
}
});
// MAKE NAVBAR ACTIVE
$('li').removeClass('active');
$('a[href="'+url+'"]').parent().addClass('active');
//window.history.pushState('', ajaxtitle, href);
};
我的链接示例,只是为了补充:
<a href="projects" ajaxtitle="Projects">PROJECTS</a>
要让我的AJAX调用链接工作,我需要做些什么?