0

这会将内容加载到页面并更改 URL。问题是后退按钮和前进不会再次加载内容。他们只更改 URL。我怎样才能让他们加载功能?

 <a onclick="load_content('p1');" >Page1</a>
 <a onclick="load_content('p2');" >Page2</a>



function load_content(name)
    {

    window.history.pushState(null, null, "/?page=" + name + "");

    var xmlhttp;
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xmlhttp.onreadystatechange=function()
      {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {

        document.getElementById("response").innerHTML=xmlhttp.responseText;

        }
      }
    xmlhttp.open("GET","load_content.php?name=" + name + "",true);
    xmlhttp.send();

    }
4

1 回答 1

0

在 中history.pushState,保存所有动态的相关数据,然后popState像使用 AJAX 一样监听事件并替换所有元素。

于 2013-05-19T12:02:04.193 回答