我有一個函數,它使用ajax來加載我的一個網站的內容。點擊功能無法正常工作或改變時
這樣做的效果很好,但在某些部分,因爲帶來了新的內容,有更多的鏈接我需要使點擊功能成爲一個實時點擊功能,但是當我做ajax停止工作,它只是改變頁面正常(沒有Ajax)
這裏是我的代碼
function ajaxLoads(){
var $btn = $('a.ajax-btn');
var $container = $('#load');
var siteTitle = document.title;
//var $artistBG = $('#artist-viewport img');
$btn.click(function(e){
console.log();
e.preventDefault();
$btn.removeClass('selected');
//$artistBG.removeClass('top');
var sourceTarget = $(this).data('page'),
slideWidth = $container.width(),
$this = $(this),
$background = $this.data('background'),
pageUrl=$this.attr('href'),
pageTitle = $this.html();
//$changeTo = $("#artist-viewport img[data-background='"+ $background +"']");
$this.addClass('selected');
//$changeTo.addClass('top');
$container.animate({'right':-slideWidth}, 300, 'easeInExpo');
$container.load(pageUrl+" "+sourceTarget, function(){
subNav();
$("html, body").animate({ scrollTop: 0 }, 500, 'easeInExpo', function(){
$container.animate({'right':0}, 300, 'easeInExpo');
});
var pageContent = $(this).html();
window.history.pushState({"html":pageContent, "pageTitle": pageTitle},"", pageUrl);
//document.title=pageTitle+" :: "+siteTitle;
}); // end load function
}); // end click function
window.onpopstate = function(e){
if(e.state){
$container.css('opacity', 0).html(e.state.html).fadeTo('fast', 1);
document.title=e.state.pageTitle+" :: "+siteTitle;
}
};
}
我曾試圖改變$btn.click(function(e) to $btn.live('click', function(e) and to $btn.on('click', function(e)
您可以使用委託,直播或上。這裏是例子http://api.jquery.com/delegate/ – ajay 2013-03-25 11:10:16