search
Homephp教程php手册TP+Pjax Demo 实现无刷新改变URL的方式

TP3.1.3+Pjax Demo 实现无刷新改变URL的方式
Pjax相关原理及用法可以从下面的网站查看。
welefen版本:https://github.com/welefen/pjax
defunkt版本:https://github.com/defunkt/jquery-pjax [Demo用的是这个]

支持Pjax功能的浏览器(HTML5):http://caniuse.com/#search=pushstate

TP+Pjax Demo实现方法:
./Lib/Common.Action.class.php 【临时开启模板layout(true);的用法见:http://www.thinkphp.cn/info/183.html第三种方式】<?php <br /> class CommonAction extends Action {<br>     protected function render($data) {<br>         $this->assign('data', $data); //控制器传值到模板<br> <br>         if (array_key_exists('HTTP_X_PJAX', $_SERVER) && $_SERVER['HTTP_X_PJAX']) {<br>             $this->display('','','','','pjax/'); //浏览器支持Pjax功能,直接渲染输出页面。Bug fix: 兼容非调试模式<br>         } else {<br>             layout(true); //开启模板<br>             $this->display(); //浏览器不支持Pjax功能或F5刷新页面,使用默认的链接响应机制(加载模板)<br>         }<br>     }<br> }./Lib/IndexAction.class.php (继承于CommonAction)<?php <br /> class IndexAction extends CommonAction {<br>     public function index()  {<br>         $data['name'] = 'ThinkPHP+Pjax Demo';<br>         $this->render($data);<br>     }<br> <br>     public function about()  {<br>         $data['name'] = '测试传参。';<br>         $this->render($data);<br>     }<br> }./TPL/layout.html (模板中Pjax布署)    <script><br /> $(document).ready(function(){<br /> $(&#039;#loading&#039;).hide(); //隐藏loading<br /> });<br /> $(document).pjax(&#039;a&#039;, &#039;#pjax-container&#039;); //内容替换的容器<br /> $(document).on(&#039;pjax:send&#039;, function() {<br /> $(&#039;#loading&#039;).show(); //显示loading<br /> });<br /> $(document).on(&#039;pjax:complete&#039;, function() {<br /> $(&#039;#loading&#039;).fadeOut(1000); //隐藏loading效果<br /> });<br /> </script>

附件 TPDemo2.zip ( 806.39 KB 下载:355 次 )

AD:真正免费,域名+虚机+企业邮箱=0元

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment