編程(Programming)是編定程序的中文簡稱,就是讓計算機代碼解決某個問題,對某個計算體系規定一定的運算方式,使計算體系按照該計算方式運行,并最終得到相應結果的過程。為了使計算機能夠理解(understand)人的意圖,人類就必須將需解決的問題的思路、方法和手段通過計算機能夠理解的形式告訴計算機,使得計算機能夠根據人的指令一步一步去工作,完成某種特定的任務。這種人和計算體系之間交流的過程就是編程。 【實例名稱】 JS代碼實現網頁中的選項卡效果 【實例描述】 選項卡是C/S(Client/Server)應用程序中常見的控件,但網頁中并無此控件。本例學習使用JavaScript和CSS實現B/S(Browser/Server)應用程序的選項卡。 【實例代碼】 <html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>標題頁-本站(www.xue51.com)</title>
<style>
.conts{visibility:hidden}
.tab{border-top:solid thin #E0E0E0;
border-right:solid thin gray;
border-left:solid thin #E0E0E0;
font-family:Verdana;
font-size:10pt;
text-align:center;
font-weight:normal}
.selTab{border-left:solid thin white;
border-top:solid thin white;
border-right:solid thin black;
font-weight:bold;
text-align:center}
.conts {visibility:hidden}
.tab{border-top:solid thin #E0E0E0;
border-right:solid thin gray;
border-left:solid thin #E0E0E0;
font-family:Verdana;
font-size:10pt;
text-align:center;
font-weight:normal}
.selTab{border-left:solid thin white;
border-top:solid thin white;
border-right:solid thin black;
font-weight:bold;
text-align:center}
</style>
<script language="JavaScript">
function public_Labels(label1, label2, label3, label4, label5, label6, label7){
t1.innerText = label1;
t2.innerText = label2;
t3.innerText = label3;
t4.innerText = label4;
t5.innerText = label5;
t6.innerText = label6;
t7.innerText = label7;
}
//切換選項卡時顯示選項卡的內容
function public_Contents(contents1, contents2,
contents3, contents4, contents5, contents6, contents7){
t1Contents.innerHTML = contents1;
t2Contents.innerHTML = contents2;
t3Contents.innerHTML = contents3;
t4Contents.innerHTML = contents4;
t5Contents.innerHTML = contents5;
t6Contents.innerHTML = contents6;
t7Contents.innerHTML = contents7;
init();
}
//默認顯示選項卡1
function init(){
tabContents.innerHTML = t1Contents.innerHTML;
}
//更換選項卡時的方法
var currentTab;
var tabBase;
var firstFlag = true;
function changeTabs(){
if(firstFlag == true){
currentTab = t1;
tabBase = t1base;
firstFlag = false;
}
//當用戶單擊選項卡時,修改樣式及內容
if(window.event.srcElement.className == "tab")
{
currentTab.className = "tab";
tabBase.style.backgroundColor = "white";
currentTab = window.event.srcElement;
tabBaseID = currentTab.id + "base";
tabContentID = currentTab.id + "Contents";
tabBase = document.all(tabBaseID);
tabContent = document.all(tabContentID);
currentTab.className = "selTab";
tabBase.style.backgroundColor = "";
tabContents.innerHTML = tabContent.innerHTML;
}}
</script>
</head>
<body BGCOLOR="#c0c0c0" onclick="changeTabs()"
onload="init()">
<div STYLE="position:absolute; top:20; height:350;
width:500; left:65; border:none thin gray">
<table STYLE="width:600; height:350" CELLPADDING="0"
CELLSPACING="0" bgcolor="c0c0c0">
<tr><td ID="t1" CLASS="selTab" HEIGHT="25">選項 1</td>
<td ID="t2" CLASS="tab">選項 2</td>
<td ID="t3" CLASS="tab">選項 3</td>
<td ID="t4" CLASS="tab">選項 4</td>
<td ID="t5" CLASS="tab">選項 5</td>
<td ID="t6" CLASS="tab">選項 6</td>
<td ID="t7" CLASS="tab">選項 7</td>
</tr><tr>
<td ID="t1base" STYLE="height:2; border-left:solid thin white"></td>
<td ID="t2base" STYLE="height:2; background-color:white"></td>
<td ID="t3base" STYLE="height:2; background-color:white"></td>
<td ID="t4base" STYLE="height:2; background-color:white"></td>
<td ID="t5base" STYLE="height:2; background-color:white"></td>
<td ID="t6base" STYLE="height:2; background-color:white"></td>
<td ID="t7base" STYLE="height:2; background-color:white;
border-right:solid thin white"></td>
</tr><tr><td HEIGHT="*" COLSPAN="7" ID="tabContents"
STYLE="border-left:solid thin white;border-bottom:solid
thin white;border-right:solid thin white">sample
contents</td></tr></table></div><div CLASS="conts" ID="t1Contents">
<p align="center">Tab1的內容 </p>
</div><div CLASS="conts" ID="t2Contents">
<p align="center">Tab2的內容 </p>
</div><div CLASS="conts" ID="t3Contents">
<p align="center">Tab3的內容 </p>
</div><div CLASS="conts" ID="t4Contents">
<p align="center">Tab4的內容 </p>
</div><div CLASS="conts" ID="t5Contents">
<p align="center">Tab5的內容 </p>
</div><div CLASS="conts" ID="t6Contents">
<p align="center">Tab6的內容 </p>
</div><div CLASS="conts" ID="t7Contents">
<p align="center">Tab7的內容 </p></div>
</body>
</html>
【運行效果】
 【難點剖析】 本例的重點是選項卡的樣式和切換選項卡的事件。其中使用了“window.event.srcElement”來獲取選擇的Tab。 【源碼下載】 為了JS代碼的準確性,請點擊:網頁中的選項卡 進行本實例源碼下載
使用編程語言寫的程序,由于每條指令都對應計算機一個特定的基本動作,所以程序占用內存少、執行效率高。 |