DIV CSS 佈局教程網

一個簡單實用的 JavaScript 選單範例
編輯:關於JavaScript     

這是一個結構清楚、容易改造的 JavaScript 選單範例。它把樣式抽成 class、把資料抽成陣列,因此要換配色或改選項,都只要動對應的一小塊即可。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>AgetimeMenu Demo</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style>
 .agetime_bar{
  position:absolute;top:0px;left:0px;height:22px;width:100%;border:1px outset;background-color:RGB(212,208,200);z-index:98;
 }
 .agetime_barItem{
  width:60px;height:20px;border:1px solid RGB(212,208,200);text-align:left;padding-left:10px;
  background:RGB(212,208,200);color:#000000;font-size:9pt;
 }
 .agetime_barItemDown{
  width:60px;height:20px;border:1px inset RGB(212,208,200);text-align:left;padding-left:10px;
  background:#F0F0F0;color:#000000;font-size:9pt;
 }
 .agetime_barItemHover{
  width:60px;height:20px;border:1 outset;text-align:left;padding-left:10px;
  background:#F0F0F0;color:#000000;font-size:9pt;
 }
 .agetime_pad{
  cursor:default;font-size:9pt;width:100%;
 }
 .agetime_padItem{
  width:100%;height:18px;border:1px solid RGB(212,208,200);text-align:left;padding-left:10px;
  background:RGB(212,208,200);color:#000000;font-size:9pt;
 }
 .agetime_padItemFalse{
  padding-left:10px;font-size:9pt; color:#808080;
 }
 .agetime_padItemFalseHover{
  padding-left:10px;font-size:9pt; color:#808080;background-color:#333366;
 }
 .agetime_padItemHover{
  width:100%;height:18px;text-align:left;padding-left:10px;
  background-color:#333366;color:#FFFFFF;font-size:9pt;
 }
 .agetime_padItemDown{
  width:100%;height:18px;text-align:left;padding-left:10px;border:1px inset;
  background-color:#9999CC;color:#FFFFFF;font-size:9pt;
 }
 .agetime_hr{
  border:1px inset;
 }
 .agetime_board{
  background-color:RGB(212,208,200);border:2px outset;
 }
</style>
</head>
<body>
<script language="javascript">
 var menu = agetimeMenu("agetime",
  [
   [
    ["文件",null,null,true,"打開文件"],  // 顯示文字, 方法, 命令, 狀態, 狀態列顯示文字
    ["打開",null,null,false,"打開文件"],
    ["--"],
    ["你好","js","alert('Hello')",true,"一聲問候"],
    ["新窗口","ABC","about:blank",true,"彈出ABC窗口"],
    ["空白",null,"about:blank",true,"在當前窗口顯示空白頁"]
   ],
   [
    ["編輯",null,null,false,"打開文件"],
    ["撤消",null,null,true,"打開文件"],
    ["重做",null,null,true,"打開文件"]
   ],
   [
    ["文件","js","alert('無子菜單')",true,"打開文件"]
   ]
  ]
 );

從這段程式碼可以看出它的設計取捨:選單項目用巢狀陣列描述,每一項依序代表顯示文字、觸發方式、要執行的命令、是否啟用,以及狀態列提示文字;若某一項只有 "--",就當成分隔線。樣式則全部集中在 agetime_ 前綴的 class 中,並為 hover 與按下狀態各自準備了對應規則。

這種「資料與樣式分離」的做法值得沿用:要調整外觀就改 CSS,要調整選單內容就改陣列,兩者互不干擾,後續維護成本也低得多。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved