使用jQuery刷新div加载的div? [英] Refresh html loaded on div using jQuery?
问题描述
我正在制作一个网站,并且使用jQuery将我的html页面加载到div中,所以主索引html保持不变。问题是,如果用户正在浏览加载到div中的html并尝试刷新其浏览器(F5),则内容会丢失,因为它只有一个页面(index.html)。
I am making a website and I use jQuery to load my html pages inside divs, so the main index html remains intact that way. The problem is that if a user is browsing an html that loads inside a div and tries to refresh his browser(F5) the content gets "lost" since it's only one page(index.html).
有没有办法解决这个问题?
Is there a way to get around this?
<a onclick='$("#content").load("page1.html");'>Page1</a>
<div id="content"></div>
推荐答案
有两种解决方案。如果用户点击链接,则会向网址添加哈希标签。例如:
There's two solutions for that. If a user clicks a link, you add a hashtag to the url. For example:
<a onclick='$("#content").load("page1.html"); window.location.hash = "page1.html";'>Page1</a>
然后,在jQuery中,当用户访问网站时检查hashtag:
Then, in your jQuery, you check for a hashtag when a user visits the site:
$(function() {
if (window.location.hash.length) {
var hash = window.location.hash.replace('#', '');
$("#content").load(hash);
}
});
其次,您可以使用cookie,但我认为散列方式也可以正常工作:)
Secondly, you could use cookies but I think the hash way works fine, too :)
这篇关于使用jQuery刷新div加载的div?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!