2016-05-16 27 views
0

当用户点击其中一个按钮时,他们通过ajax更新页面的内容,然后按钮被隐藏,一些新的按钮可供用户使用。问题是当我点击后退按钮时,它不会回到上一个功能。我希望用户回到前一组按钮,这里是我的代码:如何返回到jQuery的功能

$(".buttonset1").click(function() { 
    $('#div1').css("display","none"); 
    id = $(this).attr("value"); 
    query = 'ajax.php?id='+id; 
    myQuery(query); 
    $('#div2').css("display","block"); 
}); 

$(".buttonset2").click(function() { 
    $('#div2').css("display","none"); 
    value = $(this).attr("value"); 
    query = 'ajax.php?id='+id+'&var1='value; 
    myQuery(query); 
    $('#div3').css("display","block"); 
}); 

现在DIV2显示给用户....更改为MyQuery功能做了ajax查询

function myQuery(url) { 
    if (window.XMLHttpRequest) { 
     xmlHttp = new XMLHttpRequest(); 
    } else { 
     xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
    xmlHttp.onreadystatechange = function() { 
     if (xmlHttp.readyState == 4 && xmlHttp.status == 200) { 
      document.getElementById("sql").innerHTML = xmlHttp.responseText; 
     } 
    } 
    xmlHttp.open('GET', url, true); 
    xmlHttp.send(); 
} 
+0

按下浏览器的后退按钮会去到以前的网址,JavaScript代码将重新加载(和变量数据丢失),如果你重新加载页面。对于JavaScript函数没有“后退”,你必须在更新之前存储你以前的变量,并为自己编写一个函数,让它恢复(不要点击后退浏览器按钮思想) –

+0

如何使用history.pushstate ...有没有其他方法可以做到这一点? –

+0

你在哪里看到pushState()方法?我在猜测https://developer.mozilla.org/en-US/docs/Web/API/History_API#The_pushState%28%29_method? –

回答

0

解决这是非常简单的

History.Adapter.bind(window,'statechange',function(){ // Note: We are using statechange instead of popstate 
    var State = History.getState(); // Note: We are using History.getState() instead of event.state 
    var url = State.url;  
    myQuery(url); 
});