window.history.pushState函数在使用时会重新加载页面

时间:2020-06-02 18:03:56

标签: javascript php html jquery ajax

我正在尝试不重新加载而翻页,我阅读了其他可以使用的问题

    window.history.pushState()

不重新加载页面,但仍在重新加载页面。

我也尝试使用此功能,但一直给我一个语法错误

  function changeurl(url, title) {
   var new_url = '/' + url;
   window.history.pushState('data', 'Title', new_url);
   document.title = title;
    }

发生语法错误,它显示为:“意外令牌,”

这是我的HTML的示例:

 <a class="ac" href=""  onclick="window.history.pushState('data', 'Title', 'index?a=recieved');"></a></td>
        <li>

         <a class="" href="index?b=favorites" onclick=""></a></td>
        </li>
        <li>
           <a class="" href="index?a=sent" onclick=""></a></td>
        </li>

我该怎么做才能不重新加载我的页面?

1 个答案:

答案 0 :(得分:0)

在标签a中尝试使用 href =“ javascript:void(0)”

<a class="ac" href="javascript:void(0)"  
onclick="window.history.pushState('data', 'Title', 'index?a=recieved');">
Link
</a>