Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/441.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用history.pushState更改url以使用jQuery Ajax创建页面转换?_Javascript_Jquery_Transition - Fatal编程技术网

Javascript 如何使用history.pushState更改url以使用jQuery Ajax创建页面转换?

Javascript 如何使用history.pushState更改url以使用jQuery Ajax创建页面转换?,javascript,jquery,transition,Javascript,Jquery,Transition,我正在尝试使用window.history.pushState()实现无缝页面转换,以更改地址栏中的url。我遵循了此操作,但pushState引发了以下错误:Uncaught DomeException:未能对“History”执行“pushState”:无法在源代码为“null”且URL为“code”的文档中创建URL为[link]的历史状态对象 有人知道如何解决这个问题吗。我试图实现的是,获取所单击按钮的href并更改url,例如:http://www.example.com/index.

我正在尝试使用window.history.pushState()实现无缝页面转换,以更改地址栏中的url。我遵循了此操作,但pushState引发了以下错误:
Uncaught DomeException:未能对“History”执行“pushState”:无法在源代码为“null”且URL为“code”的文档中创建URL为[link]的历史状态对象

有人知道如何解决这个问题吗。我试图实现的是,获取所单击按钮的href并更改url,例如:
http://www.example.com/index.html
http://www.example.com/about.html

$('.button').on('click', function(e) {
        e.preventDefault();

        var href = $(this).attr('href');
        window.history.pushState(null, null, href); 

        $.ajax({
            url: href,
            success: function(data) {
                // Do animation and change content
            }
        });
    });

如果您试图从
http://www.example.com/index.html

$('.button').on('click', function(e) {
        e.preventDefault();

        var href = $(this).attr('href');
        window.history.pushState(null, null, href); 

        $.ajax({
            url: href,
            success: function(data) {
                // Do animation and change content
            }
        });
    });
null
origin是一个URL以
file:
开头的页面

您需要在一个原点不是
null
的页面中运行JS。i、 e.在具有HTTP(S)URL的web服务器上