我在我的网页上工作,找不到我的问题的答案。
我正在使用window.history.pushstate和onpopstate事件,这是我的代码:
changeContent = function(href) {
window.history.pushState({}, '', href);
if (href == "/") {
$('#main').load('main.html');
} else {
$('#main').html('<iframe id="gallery" src="photos'+href+'/index.html"></iframe>');
}
};
$(window).on("popstate", function(e) {
changeContent(location.pathname);
});
$(document).on("click", ".galleryLink", function() {
var href = $( this ).attr( "href" );
changeContent(href)
return false;
});
即
google page - &gt;我的主页 - &gt; gallery - &gt;后退按钮 - &gt;我的主页 - &gt;后退按钮 - &gt;我的主页 - &gt;后退按钮 - &gt;我的主页
发生了什么事?
答案 0 :(得分:4)
知道了!
行:
window.history.pushState({}, '', href);
应该在里面
$(document).on("click", ".galleryLink", function()
我把它放在里面
changeContent = function(href)
这是我的错......: - )
答案 1 :(得分:1)
这里说,jQuery捕获popstate
事件时遇到问题,你应该使用原生的onpopstate
。
jQuery bind popstate event not passed
但是我想知道它为什么会运作一次?