快速接入Pjax

Pjax是jQuery的一个插件,Pjax即pushState + Ajax.

引用

</head>标记结束前加入.

1
<script type="text/javascript" src="https://unpkg.com/jquery-pjax@2.0.1/jquery.pjax.js"></script>

容器

使用div标签来规定所需替换的内容.

1
2
3
<div id="container">
内容
</div>

处理

</body>标记结束前加入.

1
2
3
4
5
6
7
8
9
10
11
$(document).pjax('a[target!=_blank]', {
container: '#container', // 容器id
fragment: '#container', // 容器id
timeout: 6000 //超时时间(单位ms)
}).on('pjax:start', function() {
// 与服务器连接建立后触发
}).on('pjax:success', function() {
// 内容替换成功后触发
}).on('pjax:end', function() {
// 所有事件结束后触发
});