如何关闭ajax加载的页面并带回旧内容?

我正在开发一个单页组合网站,它将从单独的html文件中提取项目。 所以,目前我有这个代码将新的URL(特别是#project div)加载到当前的div #port-content

我的custom.js的一部分如下:

 var hash = window.location.hash.substr(1); var href = $('.ajax').each(function(){ var href = $(this).attr('href'); if(hash==href.substr(0,href.length-5)){ var toLoad = hash+'.html #project'; $('#port-content').load(toLoad) } }); $('a.port-more').click(function(){ var toLoad = $(this).attr('href')+' #project'; $('#port-content').hide('normal',loadContent); window.location.hash = $(this).attr('href').substr(0,$(this).attr('href').length-5); function loadContent() { $('#port-content').load(toLoad,'',showNewContent()) } function showNewContent() { $('#port-content').show('normal'); } return false; }); 

如何关闭此内容并恢复原来的#port-content div。 我尝试创建一个相反的新function,但它没有用。

有任何想法吗?

 var HTMLstorage = ""; $("#something").on("click", function() { var HTMLstorage = $("#someDivWithOldContent").html(); $("#someDivWithOldContent").html(yourNewHTML); } 

捕获22就是你可能会失去你的处理程序分配,具体取决于你如何分配它们

在替换之前基本上捕获HTML。 如上所述,一旦恢复它,您可能需要重新分配处理程序

使用.data()声音就像是正确的选项。

 function loadContent() { if(!$('#port-content').data('default')) $("#port-content").data('default', $("#port-content").html()); $('#port-content').load(toLoad,'',showNewContent()) } // and create another function to load the default function loadDefault() { $('#port-content').html($("port-content").data('default')); }