Javascript 历史Pushstate和Popstate工作不正常

Javascript 历史Pushstate和Popstate工作不正常,javascript,jquery,html5-history,Javascript,Jquery,Html5 History,我应该做什么来适应这种情况,就像我回到过去,然后继续前进 注意:我已经看到了许多关于stackoverflow的答案,我知道这可能是重复的,我已经尝试了许多解决方案。在处理代码之后。。。我达到了这个目的 $( document ).ready(function() { $(".a-menu").click(function(event){ event.preventDefault(); window.history.pushState({ content:

我应该做什么来适应这种情况,就像我回到过去,然后继续前进


注意:我已经看到了许多关于stackoverflow的答案,我知道这可能是重复的,我已经尝试了许多解决方案。

在处理代码之后。。。我达到了这个目的

$( document ).ready(function() {
    $(".a-menu").click(function(event){
        event.preventDefault();
        window.history.pushState({ content: $('.page-content').html() }, 
        pageData['header'], $(this).attr('href'));
    });

    window.onpopstate = function(event){
        var d = event.state;
        $('.page-content').html(d.content); //d.content doesn't give anything for last back attempt.
    };
});
$(文档).ready(函数(){
var getHtml=$('.page content').html();
$(“.a-menu”)。单击(函数(事件){
event.preventDefault();
var thisref=$(this.attr('href');
getHtml=$('.page content').html();
//添加新内容
$('.page content').html(''+thisref+'');
if(window.location.href.indexOf(thishhref)=-1){
window.history.pushState({content:$('.page content').html()},null,thisref);
}
});
window.addEventListener('popstate',函数(事件){
var state=event.state==null?getHtml:event.state.content;
console.log(状态);
$('.page content').html(状态);
});
});
这就是它应该如何工作的

$(文档).ready(函数(){
var getHtml=$('.page content').html();//获取内容div html content
$(“.a-menu”)。单击(函数(事件){//

当前页面是:index.html
在围绕代码工作之后…我达到了这个目标

$( document ).ready(function() {
    $(".a-menu").click(function(event){
        event.preventDefault();
        window.history.pushState({ content: $('.page-content').html() }, 
        pageData['header'], $(this).attr('href'));
    });

    window.onpopstate = function(event){
        var d = event.state;
        $('.page-content').html(d.content); //d.content doesn't give anything for last back attempt.
    };
});
$(文档).ready(函数(){
var getHtml=$('.page content').html();
$(“.a-menu”)。单击(函数(事件){
event.preventDefault();
var thisref=$(this.attr('href');
getHtml=$('.page content').html();
//添加新内容
$('.page content').html(''+thisref+'');
if(window.location.href.indexOf(thishhref)=-1){
window.history.pushState({content:$('.page content').html()},null,thisref);
}
});
window.addEventListener('popstate',函数(事件){
var state=event.state==null?getHtml:event.state.content;
console.log(状态);
$('.page content').html(状态);
});
});
这就是它应该如何工作的

$(文档).ready(函数(){
var getHtml=$('.page content').html();//获取内容div html content
$(“.a-menu”)。单击(函数(事件){//
当前页面是:index.html
试试这个

$( document ).ready(function() {
     var getHtml = $('.page-content').html();
     $(".a-menu").click(function(event){
        event.preventDefault();
        var ThisHref = $(this).attr('href');
        getHtml = $('.page-content').html();

        // add A new Content 
        $('.page-content').html('<div>'+ThisHref+'</div>');

        if(window.location.href.indexOf(ThisHref) === -1){
           window.history.pushState({content: $('.page-content').html()},null, ThisHref);
        }
    });
    window.addEventListener('popstate', function(event) {
       var state = event.state == null ? getHtml : event.state.content;
       console.log(state);
       $('.page-content').html(state);        
    });
});
试试这个

$( document ).ready(function() {
     var getHtml = $('.page-content').html();
     $(".a-menu").click(function(event){
        event.preventDefault();
        var ThisHref = $(this).attr('href');
        getHtml = $('.page-content').html();

        // add A new Content 
        $('.page-content').html('<div>'+ThisHref+'</div>');

        if(window.location.href.indexOf(ThisHref) === -1){
           window.history.pushState({content: $('.page-content').html()},null, ThisHref);
        }
    });
    window.addEventListener('popstate', function(event) {
       var state = event.state == null ? getHtml : event.state.content;
       console.log(state);
       $('.page-content').html(state);        
    });
});

它把我扔了出去。它直接指向窗口的原点。@user6803164不需要
prev_state
变量,您可以删除它。.回答updated@user6803164我写了一篇关于代码的评论,让你知道发生了什么。祝你有一个美好的一天:-)它把我扔了出去。它直接指向窗口的原点。@user6803164不需要
prev_state
变量,您可以删除它。.回答updated@user6803164我写了一篇关于代码的评论,让你知道发生了什么。祝你有一个美好的一天:-)它把我扔出去。它直接指向窗户的原点。它把我扔出去。它直接指向窗户的原点。