選項卡(Tab)是很常見的介面元件。這份寫法有個不錯的設計:用一個函數處理三組不同結構的選項卡,透過參數指定標籤名、內容容器名、以及是否用 class 篩選,做到一定程度的通用。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="https://www.w3.org/1999/xhtml" lang="zh-CN" xml:lang="zh-CN">
<head>
<title>選項卡</title>
<style type="text/css">
<!--
* { margin:0; padding:0; font-size:12px; font-weight:normal; }
.jj { font-weight:bolder!important; }
.box { border-top-color:#c00!important; }
.pr { color:#060!important; }
/* 樣式一:標籤橫排在上,內容疊在下方 */
#tab01 { position:relative; width:336px; height:88px; padding-top:15px; margin:50px; overflow:hidden; }
#tab01 h3 { position:relative; z-index:2; float:left; height:14px; padding:0 7px 0 8px;
margin-left:-1px; border-left:solid 1px #ccc; border-right:solid 1px #fff;
text-align:center; background:#fff; cursor:pointer; }
#tab01 h3.up { height:18px; padding:5px 7px 0 7px; margin:-6px 0 0 0;
border:solid #ccc; border-width:1px 1px 0; color:#c00; }
#tab01 div { display:none; position:absolute; left:0; top:32px; z-index:1;
width:324px; height:54px; padding:5px; border:solid 1px #ccc; color:#666; }
#tab01 div.up { display:block; }
/* 樣式二:標籤縱排,用 ol 當內容容器 */
#tab02 { position:relative; width:200px; margin:50px; border:solid #ccc; border-width:0 1px 1px; }
#tab02 h4 { height:18px; line-height:18px; border:solid #ccc; border-width:1px 0;
margin-bottom:-1px; text-align:center; background:#f6f6f6; cursor:pointer; }
#tab02 h4.up { color:#c00; }
#tab02 ol { display:none; height:54px; padding:5px; color:#666; }
#tab02 ol.up { display:block; }
/* 樣式三:內容浮在標籤右側 */
#tab03 { position:relative; width:100px; margin:50px; }
#tab03 h3 { position:relative; z-index:1; height:16px; padding-top:4px; margin-bottom:-1px;
border:solid #ccc; border-width:1px 0 1px 1px; text-align:center;
font-family:宋體; background:#eee; cursor:pointer; }
#tab03 h3.up { z-index:3; color:#c00; background:#fff; }
#tab03 div.tab { display:none; position:absolute; left:99px; top:0; z-index:2;
width:300px; height:200px; padding:5px; border:solid 1px #ccc; color:#666; }
#tab03 div.tab.up { display:block; }
-->
</style>
</head>
<body>
<div id="tab01">
<h3>首頁</h3>
<div class="jj"><p>嘿嘿,無視 div 原始 class 值。</p></div>
<h3 class="pr">測試</h3>
<div><p>繼續無視 h3 原始 class 值。</p></div>
<h3>無聊</h3>
<div><p>h3 沒有值也可以~</p></div>
<h3 class="box">傻蛋</h3>
<div><p>div 沒有值一樣可以~</p></div>
</div>
<div id="tab02">
<h4>首頁</h4>
<ol class="pr"><li>嘿嘿,無視容器原始 class 值。</li></ol>
<h4 class="box">測試</h4>
<ol><li>繼續無視 h3 原始 class 值。</li></ol>
<h4>無聊</h4>
<ol><li>h3 沒有值也可以~</li></ol>
<h4 class="bb">傻蛋</h4>
<ol><li>div 沒有值一樣可以~</li></ol>
</div>
<div id="tab03">
<h3>首頁</h3>
<div class="tab"><p>嘿嘿,無視 h3 原始 class 值。</p></div>
<h3>測試</h3>
<div class="tab wushi"><p>繼續無視 div 原始 class 值。</p></div>
<h3>無聊</h3>
<div class="tab"><p>h3 沒有值也可以~</p></div>
<h3 class="box">傻蛋</h3>
<div class="tab tab123"><p>class 值相似一樣也可以~</p>
<div><p>指定 class 後,即使再多一個 div 也行。</p></div>
</div>
</div>
<script type="text/javascript">
<!--
// 小工具:取得容器內指定標籤的集合
function Pid(id, tag){
if(!tag){
return document.getElementById(id);
}
else{
return document.getElementById(id).getElementsByTagName(tag);
}
}
// id : 容器 id
// hx : 標籤使用的標籤名,如 'h3'
// box : 內容容器使用的標籤名,如 'div' 或 'ol'
// iClass : 內容容器的 class 篩選條件,可留空
// s : 觸發事件,預設 onmouseover
// pr : 預設展開第幾個(0 起算)
function tab(id, hx, box, iClass, s, pr){
var hxs = Pid(id, hx);
var boxs = Pid(id, box);
if(!iClass){
boxsClass = boxs; // 不指定 class:直接用全部容器
}else{
var boxsClass = []; // 指定 class:只挑符合的那幾個
for(i = 0; i < boxs.length; i++){
if(boxs[i].className.match(/\btab\b/)){
boxsClass.push(boxs[i]);
}
}
}
if(!pr){ go_to(0); yy(); } // 未指定則預設展開第一個
else { go_to(pr); yy(); }
// 綁定事件
function yy(){
for(var i = 0; i < hxs.length; i++){
hxs[i].temp = i; // 把索引存到元素上,供回呼使用
if(!s){
s = "onmouseover";
hxs[i][s] = function(){ go_to(this.temp); };
}else{
hxs[i][s] = function(){ go_to(this.temp); };
}
}
}
// 切換顯示
function go_to(pr){
for(var i = 0; i < hxs.length; i++){
// 首次執行時把原始 class 存起來,作為還原的依據
if(!hxs[i].tmpClass){
hxs[i].tmpClass = hxs[i].className += " ";
boxsClass[i].tmpClass = boxsClass[i].className += " ";
}
if(pr == i){
hxs[i].className += " up"; // 展開狀態:標籤
boxsClass[i].className += " up"; // 展開狀態:內容
}else{
hxs[i].className = hxs[i].tmpClass; // 還原成原始 class
boxsClass[i].className = boxsClass[i].tmpClass;
}
}
}
}
tab("tab01", "h3", "div", "", "onclick", 2);
tab("tab02", "h4", "ol");
tab("tab03", "h3", "div", "tab");
//-->
</script>
</body>
</html>
一、用 class 的加減控制顯示。所有內容容器預設 display:none,只有加上 .up 才 display:block。JavaScript 只負責切換 class,視覺完全交給 CSS,這是關注點分離的標準做法。
二、把元素索引存在元素身上。hxs[i].temp = i 這一行解決了經典的迴圈綁定難題:事件回呼裡的 this 指向被點擊的元素,透過 this.temp 就能知道是第幾個。這比用閉包更直觀,也不必擔心作用域問題。
三、用 tmpClass 保存原始 class。切換時若直接覆寫 className,元素原本寫在 HTML 上的 class(如 class="pr")就會永久遺失。程式先把原始值存進 tmpClass 屬性,取消時再還原,因此 HTML 上既有的 class 不會被破壞。
· className.match(/\btab\b/) 用正則比對 class,其實有現成的 classList.contains('tab') 可用,語意更清楚。
· 用 onmouseover 觸發切換在行動裝置上完全無效(觸控沒有 hover)。若要跨裝置,請改用 onclick,並加上 addEventListener 綁定。
無障礙方面也有改進空間:真正的選項卡應該在容器上加 role="tablist"、標籤加 role="tab" 與 aria-selected、內容加 role="tabpanel",並支援左右方向鍵切換。這些屬性會讓螢幕閱讀器正確朗讀狀態。