tab标签页面,ajax载入
tab标签,jquery ajax载入数据库的内容
数据库只要2个字段,id和message
js文件
JavaScript Code复制内容到剪贴板
- var default_content="";
- $(document).ready(function(){
- checkURL();
- $('ul li a').click(function (e){
- checkURL(this.hash);
- });
- //filling in the default content
- default_content = $('#pageContent').html();
- setInterval("checkURL()",250);
- });
- var lasturl="";
- function checkURL(hash)
- {
- if(!hash) hash=window.location.hash;
- if(hash != lasturl)
- {
- lasturl=hash;
- // FIX - if we've used the history buttons to return to the homepage,
- // fill the pageContent with the default_content
- if(hash=="")
- $('#pageContent').html(default_content);
- else
- loadPage(hash);
- }
- }
- function loadPage(url)
- {
- url=url.replace('#page','');
- $('#loading').css('visibility','visible');
- $.ajax({
- type: "POST",
- url: "load_page.php",
- data: 'page='+url,
- dataType: "html",
- success: function(msg){
- if(parseInt(msg)!=0)
- {
- $('#pageContent').html(msg);
- $('#loading').css('visibility','hidden');
- }
- }
- });
- }
html主要内容
XML/HTML Code复制内容到剪贴板
- <div id="main" class="container">
- <ul id="navigation">
- <li><a href="#1">Page 1</a></li>
- <li><a href="#2">Page 2</a></li>
- <li><a href="#3">Page 3</a></li>
- <li><a href="#4">Page 4</a></li>
- <li><img id="loading" src="img/ajax_load.gif" alt="loading" /></li>
- </ul>
- <div class="clear"></div>
- <div id="pageContent">
- 这个是来自于<a href="http://www.freejs.net/article_tabbiaoqian_17.html">www.freejs.net</a>的测试代码,现在显示的并不是第一页而是默认内容</div>
- </div>
- <div class="clear"></div>
load_page.php
PHP Code复制内容到剪贴板
- <?php
- include_once("conn.php");
- $id =$_POST['page'];
- $id=str_replace("#","",$id);
- $sql = "select * from content where id='$id'";
- $result = mysql_query( $sql );
- $res_now = mysql_fetch_array($result);//不能用mysql_fetch_row
- echo $res_now['message'];
- ?>
原文地址:http://www.freejs.net/article_tabbiaoqian_17.html