Tab頁(yè)界面 用jQuery及Ajax技術(shù)實(shí)現(xiàn)(php后臺(tái))
更新時(shí)間:2011年10月12日 21:48:20 作者:
到了B/S開發(fā)時(shí)代,網(wǎng)頁(yè)前端布局也把Tab頁(yè)的布局形式吸收了過(guò)來(lái)。特別是和Ajax技術(shù)結(jié)合起來(lái),可以更充分發(fā)揮Tab頁(yè)的良好表現(xiàn)力和數(shù)據(jù)緩存的優(yōu)勢(shì),是一種良好的網(wǎng)頁(yè)布局形式
網(wǎng)上的Tab頁(yè)代碼很多,實(shí)現(xiàn)也大同小異 ,但代碼都顯得凌亂,若要真的用的話,必須費(fèi)勁的吃透它,才能進(jìn)行二次定制開發(fā),為我所用。實(shí)際上實(shí)現(xiàn)這個(gè)Tab頁(yè)界面非常簡(jiǎn)單,僅僅是通過(guò)Ajax技術(shù)偷偷的獲取信息,然后在一塊區(qū)域輪流顯示而已(通過(guò)顯示和隱藏層,或復(fù)用一個(gè)層,輪流向里邊填充Html數(shù)據(jù))。
自己的開發(fā)的代碼自己最清楚,用起來(lái)也應(yīng)該最順手,要擴(kuò)展的話腦子也不犯暈。代碼如下,還在不斷修改中。
代碼如下:
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd“>
<html xmlns=”http://www.w3.org/1999/xhtml“>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<title>我的Tabs選項(xiàng)卡(Ajax版本)</title>
<style type=”text/css”>
body {font-size:12px; }
#tab0,#tab1,#tab2 {background:url(images/gray.png); cursor:hand;}
</style>
<script type=”text/javascript” src=”jquery/jquery.js”></script>
<script type=”text/javascript” src=”class.js”></script>
<script type=”text/javascript” src=”my_ajax_tabs.js”></script>
<script type=”text/javascript”>
$(document).ready(function()
{
//————————-
//tabs 配置信息
var tabs = [”#tab0″, “#tab1″, “#tab2″]; //tab 數(shù)組 id
var datas = “#div_data”; //顯示區(qū)對(duì)象的id號(hào)
var event_type = “mouseover”; //觸發(fā)事件(click/dblclick/mouseover/focus/…)
var default_tab = “#tab0″;
//切換圖片
var tab_selected_bgimg = “images/green.png”;
var tab_unselected_bgimg = “images/gray.png”;
//切換文本顏色
var tab_selected_txtcolor = “#ff6600″;
var tab_unselected_txtcolor = “#666666″;
//
urls = [
“my_ajax_server.php?app=tab0″,
“my_ajax_server.php?app=tab1″,
“my_ajax_server.php?app=tab2″,
]
//**Begin:固定代碼*********************************
for (var i=0; i<tabs.length; i++)
{
$(tabs[i]).bind(event_type, handler);
}
$(default_tab).trigger(event_type);
//
function handler()
{
//初始化緩存數(shù)組
var localdatas = new Array(); //緩存瀏覽器本次數(shù)據(jù)
for (var i=0; i<tabs.length; i++)
{
localdatas[i]=”;
}
//重置所有tabs
for (var i=0; i<tabs.length; i++)
{
$(tabs[i]).css(”background-image”, “url(”+ tab_unselected_bgimg +”)”);
$(tabs[i]).css(”color”, tab_unselected_txtcolor);
}
var curr_index;
for(var i=0;i<tabs.length;i++)
{
if($(tabs[i]).attr(”id”)==$(this).attr(”id”))
{
curr_index = parseInt(i);
}
}
//
$(this).css(”background-image”, “url(”+ tab_selected_bgimg +”)”);
$(this).css(”color”, tab_selected_txtcolor);
if(localdatas[curr_index]==”)
{
//ajax獲取數(shù)據(jù)(默認(rèn)method=get)
$.ajax
({
url: urls[curr_index], //后臺(tái)處理程序
cache: false,
timeout: 20000,
error:function()
{
alert(”error while submitting”);
},
success:function(data)
{
localdatas[curr_index] = data; //緩存瀏覽器本次數(shù)據(jù)
$(datas).html(data);
}
});
}
else
{ //顯示緩存數(shù)據(jù)
$(datas).html(datas[curr_index]);
}
}
//**End:固定代碼*********************************
//—————–
});
</script>
</head>
<body>
<table border=”0″ width=”500″ height=”25″ border=”0″ align=”center” cellpadding=”0″ cellspacing=”0″>
<tr>
<td width=”97″ id=”tab0″>tab0</td>
<td width=”30″></td>
<td width=”97″ id=”tab1″>tab1</td>
<td width=”30″></td>
<td width=”97″ id=”tab2″>tab2</td>
<td width=”149″></td>
</tr>
</table>
<table border=”1″ width=”500″ height=”60″ border=”0″ align=”center” cellpadding=”0″ cellspacing=”0″>
<tr>
<td>
<div id=”div_data”></div>
</td>
</tr>
</table>
</body>
</html>
my_ajax_server.php文件:
代碼如下:
<?php
/*******************************************
* File: my_ajax_server.php
********************************************/
error_reporting(7);
set_magic_quotes_runtime(0);
$app = $_GET['app'];
switch ($app)
{
case “tab0″: //
?>
from TAB0
<?php
break;
case “tab1″: //
?>
from TAB1
<?php
break;
case “tab2″: //
?>
from TAB2
<?php
break;
default:
echo ‘my_ajax_server.php error.';
break;
}
?>
自己的開發(fā)的代碼自己最清楚,用起來(lái)也應(yīng)該最順手,要擴(kuò)展的話腦子也不犯暈。代碼如下,還在不斷修改中。
代碼如下:
復(fù)制代碼 代碼如下:
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd“>
<html xmlns=”http://www.w3.org/1999/xhtml“>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<title>我的Tabs選項(xiàng)卡(Ajax版本)</title>
<style type=”text/css”>
body {font-size:12px; }
#tab0,#tab1,#tab2 {background:url(images/gray.png); cursor:hand;}
</style>
<script type=”text/javascript” src=”jquery/jquery.js”></script>
<script type=”text/javascript” src=”class.js”></script>
<script type=”text/javascript” src=”my_ajax_tabs.js”></script>
<script type=”text/javascript”>
$(document).ready(function()
{
//————————-
//tabs 配置信息
var tabs = [”#tab0″, “#tab1″, “#tab2″]; //tab 數(shù)組 id
var datas = “#div_data”; //顯示區(qū)對(duì)象的id號(hào)
var event_type = “mouseover”; //觸發(fā)事件(click/dblclick/mouseover/focus/…)
var default_tab = “#tab0″;
//切換圖片
var tab_selected_bgimg = “images/green.png”;
var tab_unselected_bgimg = “images/gray.png”;
//切換文本顏色
var tab_selected_txtcolor = “#ff6600″;
var tab_unselected_txtcolor = “#666666″;
//
urls = [
“my_ajax_server.php?app=tab0″,
“my_ajax_server.php?app=tab1″,
“my_ajax_server.php?app=tab2″,
]
//**Begin:固定代碼*********************************
for (var i=0; i<tabs.length; i++)
{
$(tabs[i]).bind(event_type, handler);
}
$(default_tab).trigger(event_type);
//
function handler()
{
//初始化緩存數(shù)組
var localdatas = new Array(); //緩存瀏覽器本次數(shù)據(jù)
for (var i=0; i<tabs.length; i++)
{
localdatas[i]=”;
}
//重置所有tabs
for (var i=0; i<tabs.length; i++)
{
$(tabs[i]).css(”background-image”, “url(”+ tab_unselected_bgimg +”)”);
$(tabs[i]).css(”color”, tab_unselected_txtcolor);
}
var curr_index;
for(var i=0;i<tabs.length;i++)
{
if($(tabs[i]).attr(”id”)==$(this).attr(”id”))
{
curr_index = parseInt(i);
}
}
//
$(this).css(”background-image”, “url(”+ tab_selected_bgimg +”)”);
$(this).css(”color”, tab_selected_txtcolor);
if(localdatas[curr_index]==”)
{
//ajax獲取數(shù)據(jù)(默認(rèn)method=get)
$.ajax
({
url: urls[curr_index], //后臺(tái)處理程序
cache: false,
timeout: 20000,
error:function()
{
alert(”error while submitting”);
},
success:function(data)
{
localdatas[curr_index] = data; //緩存瀏覽器本次數(shù)據(jù)
$(datas).html(data);
}
});
}
else
{ //顯示緩存數(shù)據(jù)
$(datas).html(datas[curr_index]);
}
}
//**End:固定代碼*********************************
//—————–
});
</script>
</head>
<body>
<table border=”0″ width=”500″ height=”25″ border=”0″ align=”center” cellpadding=”0″ cellspacing=”0″>
<tr>
<td width=”97″ id=”tab0″>tab0</td>
<td width=”30″></td>
<td width=”97″ id=”tab1″>tab1</td>
<td width=”30″></td>
<td width=”97″ id=”tab2″>tab2</td>
<td width=”149″></td>
</tr>
</table>
<table border=”1″ width=”500″ height=”60″ border=”0″ align=”center” cellpadding=”0″ cellspacing=”0″>
<tr>
<td>
<div id=”div_data”></div>
</td>
</tr>
</table>
</body>
</html>
my_ajax_server.php文件:
代碼如下:
復(fù)制代碼 代碼如下:
<?php
/*******************************************
* File: my_ajax_server.php
********************************************/
error_reporting(7);
set_magic_quotes_runtime(0);
$app = $_GET['app'];
switch ($app)
{
case “tab0″: //
?>
from TAB0
<?php
break;
case “tab1″: //
?>
from TAB1
<?php
break;
case “tab2″: //
?>
from TAB2
<?php
break;
default:
echo ‘my_ajax_server.php error.';
break;
}
?>
您可能感興趣的文章:
- jQuery Ajax文件上傳(php)
- JQuery打造PHP的AJAX表單提交實(shí)例
- PHP中運(yùn)用jQuery的Ajax跨域調(diào)用實(shí)現(xiàn)代碼
- 使用PHP+JQuery+Ajax分頁(yè)的實(shí)現(xiàn)
- jQuery+php實(shí)現(xiàn)ajax文件即時(shí)上傳的詳解
- jQuery+PHP+ajax實(shí)現(xiàn)微博加載更多內(nèi)容列表功能
- jquery+php+ajax顯示上傳進(jìn)度的多圖片上傳并生成縮略圖代碼
- PHP+jquery+ajax實(shí)現(xiàn)即時(shí)聊天功能實(shí)例
- PHP+jQuery+Ajax實(shí)現(xiàn)多圖片上傳效果
- PHP+jQuery+Ajax實(shí)現(xiàn)用戶登錄與退出
- php+ajax+jquery實(shí)現(xiàn)點(diǎn)擊加載更多內(nèi)容
- jQuery+Ajax+PHP“喜歡”評(píng)級(jí)功能實(shí)現(xiàn)代碼
- jQuery+Ajax+PHP實(shí)現(xiàn)“喜歡”評(píng)級(jí)功能附源碼下載
相關(guān)文章
jQuery實(shí)現(xiàn)帶展開動(dòng)畫的導(dǎo)航欄效果
這篇文章主要為大家詳細(xì)介紹了jQuery實(shí)現(xiàn)帶展開動(dòng)畫的導(dǎo)航欄效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07
jQuery事件綁定on()與彈窗實(shí)現(xiàn)代碼
這篇文章主要介紹了jQuery事件綁定on()與彈窗實(shí)現(xiàn)代碼,需要的朋友可以參考下2016-04-04
jQuery實(shí)現(xiàn)的超鏈接提示效果示例【附demo源碼下載】
這篇文章主要介紹了jQuery實(shí)現(xiàn)的超鏈接提示效果,結(jié)合實(shí)例形式對(duì)比分析了jQuery實(shí)現(xiàn)的帶有l(wèi)oading動(dòng)態(tài)圖效果的提示文字以及默認(rèn)提示文字顯示效果,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2016-09-09
jQuery實(shí)現(xiàn)錨點(diǎn)向下平滑滾動(dòng)特效示例
下面小編就為大家?guī)?lái)一篇jQuery實(shí)現(xiàn)錨點(diǎn)向下平滑滾動(dòng)特效示例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-08-08
jQuery使用hide方法隱藏頁(yè)面上指定元素的方法
這篇文章主要介紹了jQuery使用hide方法隱藏頁(yè)面上指定元素的方法,涉及jQuery使用hide隱藏元素的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03

