javascript+xml實現二級下拉式功能表一
最後更新:2017-02-28
來源:互聯網
上載者:User
javascript|xml|菜單|下拉
一、簡單說明
該菜單最大的優點是可以跨過網頁上任何標籤,甚至是架構,(一般的菜單會被select,object,flash等擋住)所以如果你有這方面的需要,那麼這個菜單是個很好的選擇。
總體而言,它有如下特性:
(1)菜單的樣式和資料都是通過xml配置
(2)菜單的樣式可以隨心所欲修改,如:
<base>
<background-color>#DDDDDD</background-color>
<background-image>url(menu_bg.jpg)</background-image>
<font-size>10pt</font-size>
<color>#000099</color>
<width>100%</width>
<height>32</height>
<text-align>center</text-align>
<cursor>default</cursor>
</base>
只要節點名稱符合各自標籤(table,tr,td之一)的style attribute即可。
(3)主功能表項目之間可以設定分割圖片,子功能表項可以設定分割線的樣式。
(4)所有圖片都由css控制,避免路徑問題。
(5)可以設定虛擬目錄的別名,為菜單連結使用絕對路徑帶來方便,如:
<context>
<path>hdsa</path>
</context>
設定該值後,使用者只需根據檔案的目錄結構寫好菜單,而不必擔心虛擬目錄的問題。
由於菜單使用了window.createPopup(),所以它被限制在IE5.5以上,而且子功能表如果是新開視窗,會被攔截。
註:用window.createPopup()製作菜單受到過前人的啟發
設定該值後,使用者只需根據檔案的目錄結構寫好菜單,而不必擔心虛擬目錄的問題。
由於菜單使用了window.createPopup(),所以它被限制在IE5.5以上,而且子功能表如果是新開視窗,會被攔截。
註:用window.createPopup()製作菜單受到過前人的啟發
=====================================================================
二、實現的兩個指令碼可以寫在一個檔案中:
(1)處理Popup視窗的指令碼
//------------------ 下面一些函數用來處理快顯視窗 ------------------ //
var pops = new Array(); // 用來儲存Popup視窗家族的數組
function CreatePopup(degree)
{
if (degree < 0) // 層數不能小於0
return null;
if (pops[degree] != null) //如果已經存在則不需建立
return pops[degree];
if (degree == 0)
pops[0] = window.createPopup(); //建立最頂層Popup視窗
else{
if (pops[degree - 1] == null)
pops[degree - 1] = CreatePopup(degree - 1) //遞迴回溯從第一層開始建立
pops[degree] = pops[degree - 1].document.parentWindow.createPopup(); //從父Popup視窗建立子Popup視窗
}
pops[degree].document.body.setAttribute("degree", degree);
return pops[degree];
}
CreatePopup(1); //建立一個2層的Popup家族
var oPopup = pops[0];
var timer = null;
/**
*顯示一級子功能表
*@param objShow - 顯示子功能表的對象
*@param strGetID - 儲存顯示內容的標籤id
*/
function showSubMenu1(objShow,strGetID) {
clearTimer();
endHideSubMenu();
var objGet = eval(document.getElementById(strGetID));
pops[1].hide();
pops[0].document.body.innerHTML = objGet.innerHTML;
pops[0].show(0,0,1,1,objShow);
var intWidth = pops[0].document.body.scrollWidth;
var intHeight = pops[0].document.body.scrollHeight;
pops[0].hide();
pops[0].show(10,objShow.offsetHeight-2,intWidth,intHeight,objShow);
}
/**
*顯示二級菜單
*@param strGetID - 儲存顯示內容的標籤id
*@param objTr - 顯示子功能表的tr對象
*/
function showSubMenu2(strGetID,objTr) {
clearTimer();
endHideSubMenu();
var objGet = eval(document.getElementById(strGetID));
pops[1].document.body.innerHTML = objGet.innerHTML;
pops[1].show(0,0,1,1,pops[0].document.body);
var intWidth = pops[1].document.body.scrollWidth;
var intHeight = pops[1].document.body.scrollHeight;
pops[1].hide();
pops[1].show(objTr.offsetWidth - 2,-2, intWidth, intHeight, objTr);
}
//隱藏一級子功能表
function hideSubMenu1(){
//pops[0].hide();
window.setTimeout("pops[0].hide()", 1000);
}
//隱藏二級子功能表
function hideSubMenu2()
{
//clearTimer();
//timer = window.setTimeout("pops[1].hide()", 1000);
pops[1].hide();
}
//隱藏所有下拉式功能表
function hideSubMenu()
{
if(pops[0] != null)pops[0].hide();
if(pops[1] != null)pops[1].hide();
}
function clearTimer()
{
if (timer != null)
window.clearTimeout(timer)
timer = null;
//alert("OK");
}
(2)讀取分析xml資料檔案,列印菜單的指令碼
//位置資訊
var m_strTitle = "您的位置->";
// -------- 主菜單樣式相關定義 ---------//
//定義主菜單樣式
var m_strMainMenuStyle = "";
//主功能表項目的樣式attribute
var m_strMainMenuItemStyle = "";
//主功能表項目樣式Property
var m_strMMItemProBase = {};
//主功能表項目滑鼠移至上方樣式Property
var m_strMMItemProMOver = {};
//功能表項目與項之間的分割圖片
var m_strMMItemSplitImg;
// -------- 子功能表樣式相關定義 ---------//
//定義子功能表樣式
var m_strSubMenuStyle = "";
//子功能表項的樣式attribute
var m_strSubMenuItemStyle = "";
//子功能表項樣式Property
var m_strSMItemProBase = {};
//子功能表項滑鼠移至上方樣式Property
var m_strSMItemProMOver = {};
//子功能表項下邊框樣式(作為分割線)
var m_strSMItemBorderB = "";
//定義虛擬目錄路徑
var m_strContextPath = "";
//菜單xml資料檔案對象
var m_doc;
//主菜單集合
var m_objMMList;
var STR_POS_FRONT = "您的位置:";
//loadMenu("main_menu.xml");
/**
*載入菜單
*@param strXMLPath - XML資料檔案所在路徑及檔案名稱
*/
function loadMenu(strXMLFile) {
m_doc = new ActiveXObject("MSXML2.DOMDocument.3.0");
m_doc.async = false;
m_doc.load(strXMLFile);
getMainMenuStyle();
getSubMenuStyle();
//得到主菜單集合
m_objMMList = m_doc.selectSingleNode("//popmenu/menus").childNodes;
//得到虛擬目錄
if( m_doc.selectSingleNode("//popmenu/context/path") ) {
m_strContextPath = m_doc.selectSingleNode("//popmenu/context/path").text;
}
//輸出主菜單
printMainMenu();
//輸出一級子功能表
printSubMenu();
}
/**
*滑鼠移入移出主菜單樣式的改變
*@param obj - 改變樣式的對象
*@param bolChg - 是否改變樣式
*/
function chgMainMenuStyle(obj,bolChg)
{
if ( bolChg ) {//滑鼠移至上方改變顏色
for( var pro in m_strMMItemProMOver ) {
obj.style[pro] = m_strMMItemProMOver[pro];
}
} else {//滑鼠移出
obj.style.border = "0";
for( var pro in m_strMMItemProBase ) {
obj.style[pro] = m_strMMItemProBase[pro];
}
}
}//end function chgMainMenuStyle(obj,i)
/**
*滑鼠移入移出子功能表樣式的改變
*@param obj - 改變樣式的對象
*@param bolChg - 是否改變樣式
*/
function chgSubMenuStyle(obj,bolChg) {
if ( bolChg ) {//滑鼠移至上方改變顏色
for( var pro in m_strSMItemProMOver ) {
obj.style[pro] = m_strSMItemProMOver[pro];
}
} else {//滑鼠移出
for( var pro in m_strSMItemProBase ) {
obj.style[pro] = m_strSMItemProBase[pro];
}
}
}//end function chgSubMenuStyle(obj,i)
/**
*構造節點樣式
*@param strPath-節點路徑
*/
function getNodeStyle(strPath) {
var strStyle = "";
if( m_doc.selectSingleNode(strPath) ) {
var nodeList = m_doc.selectSingleNode(strPath).childNodes;
for( var i = 0;i < nodeList.length;i ++ ) {
var nodeName = nodeList[i].nodeName;
var nodeText = nodeList[i].text;
strStyle += nodeName.toLowerCase() + ":" + nodeText + ";";
}
}
return strStyle;
}
/**
*構造節點Style Property
*/
function getNodeProperty(strPath) {
var arrProperty = {};
if( m_doc.selectSingleNode(strPath) ) {
var nodeList = m_doc.selectSingleNode(strPath).childNodes;
for( var i = 0;i < nodeList.length;i ++ ) {
var nodeName = nodeList[i].nodeName;
nodeName = nodeName.toLowerCase();
var nodeText = nodeList[i].text;
//由attribute到property的轉變。如background-color -> backgroundColor
var aName = nodeName.split("-");
var strProperty = aName[0];
for( var j = 1;j < aName.length;j ++ ) {
strProperty += aName[j].substring(0,1).toUpperCase() + aName[j].substr(1);
}
arrProperty[strProperty] = nodeText;
}
}
return arrProperty;
}
/**
*得到主菜單樣式
*/
function getMainMenuStyle() {
//得到主菜單樣式
m_strMainMenuStyle = getNodeStyle("//popmenu/style/main-menu/base");
//得到主功能表項目樣式
m_strMainMenuItemStyle = getNodeStyle("//popmenu/style/main-menu/menu-item/base");
//主功能表項目的property
m_strMMItemProBase = getNodeProperty("//popmenu/style/main-menu/menu-item/base");
//得到滑鼠移至上方時主功能表項目各property改變
m_strMMItemProMOver = getNodeProperty("//popmenu/style/main-menu/menu-item/mouse-over");
//得到功能表項目與項之間的分割圖片
if( m_doc.selectSingleNode("//popmenu/style/main-menu/menu-item/split-image") ) {
var node = m_doc.selectSingleNode("//popmenu/style/main-menu/menu-item/split-image");
m_strMMItemSplitImg = "background-image:" + node.text + ";";
m_strMMItemSplitImg += "background-repeat:no-repeat;background-position:center center;";
}
}//end function getMainMenuStyle()
/**
*得到主菜單樣式
*/
function getSubMenuStyle() {
//得到子功能表樣式
m_strSubMenuStyle = getNodeStyle("//popmenu/style/sub-menu/base");
//得到子功能表項樣式
m_strSubMenuItemStyle = getNodeStyle("//popmenu/style/sub-menu/menu-item/base");
//子功能表項的property
m_strSMItemProBase = getNodeProperty("//popmenu/style/sub-menu/menu-item/base");
//得到滑鼠移至上方時子功能表項各property改變
m_strSMItemProMOver = getNodeProperty("//popmenu/style/sub-menu/menu-item/mouse-over");
//得到子功能表項下邊框樣式(作為分割線)
if( m_doc.selectSingleNode("//popmenu/style/sub-menu/menu-item/split-line") ) {
var node = m_doc.selectSingleNode("//popmenu/style/sub-menu/menu-item/split-line");
m_strSMItemBorderB = "border-bottom:" + node.text + ";";
}
}//end function getSubMenuStyle()
var m_bolFirst = true;//是否首次響應菜單
document.onclick = function(){
m_bolFirst = true;
//alert(parent.frames[0]);
}
top.document.onclick = function(){
m_bolFirst = true;
}
/**
*展開各個主菜單
*@param objTd - 主菜單所在td
*@param strSubMenuID - 對應子功能表的id
*@param bolExistSubMenu - 是否存在子功能表
*/
function spreadMainMenu(objTd,strSubMenuID,bolExistSubMenu){
var strEvent = event.type.toLowerCase();
if( !pops[0].isOpen && strEvent == "mouseover" && m_bolFirst){
return;
}
if( pops[0].isOpen && strEvent == "click"){
return;
}
chgMainMenuStyle(objTd,true);
endHideSubMenu();
if( bolExistSubMenu ){
//如果有子功能表
showSubMenu1(objTd,strSubMenuID);
} else {
//hideSubMenu();
//m_bolFirst = false;//已不是第一次響應菜單
}
}
//列印主菜單
function printMainMenu()
{
var divStyle = "width:100%;";
var strHtml = "";
strHtml = "<div style='" + divStyle + "'>";
strHtml += "<table style='"+m_strMainMenuStyle+"' id='mm' cellpadding='3'><tr>";
//如果使用者佈建了分割圖片則輸出
if( m_strMMItemSplitImg && m_strMMItemSplitImg != "" ){
strHtml += "<td style='" + m_strMMItemSplitImg + "' width=2></td>";
}
var intMenuWidth = 0;
for ( var i = 0 ; i< m_objMMList.length ; i ++ )
{
var menuNode = m_objMMList[i];
var attList = menuNode.attributes;
var menuText = attList.getNamedItem("text").text;
var menuHref = attList.getNamedItem("href").text;
var menuTarget = "";
if( attList.getNamedItem("target") )
menuTarget = attList.getNamedItem("target").text;
else
menuTarget = "_self";
var intMenuWidth;
if( attList.getNamedItem("width") )
intMenuWidth = attList.getNamedItem("width").text;
var strSubMenuID = "sm" + i;
var strMouseOverEvent = "";
var strClickEvent = "";
//如果有子拉菜單,則顯示
if ( menuNode.hasChildNodes() ){
strMouseOverEvent = "chgMainMenuStyle(this,true);spreadMainMenu(this,\"" + strSubMenuID + "\",true);";
strClickEvent = strMouseOverEvent;
}else{
strMouseOverEvent = "chgMainMenuStyle(this,true);";
strClickEvent = "onClickEvent('" + menuHref + "','" + menuTarget + "','" + menuText + "');";
}
strHtml += "<td width=" + intMenuWidth + " style='" + m_strMainMenuItemStyle + "' id=mm" + i +
" " +
" >" + menuText+"</td>";
//如果使用者佈建了分割圖片則輸出
if( m_strMMItemSplitImg && m_strMMItemSplitImg != "" ){
strHtml += "<td style='" + m_strMMItemSplitImg + "' width='2'></td>";
}
}
strHtml += "<td></td>";
strHtml += "</tr>";
strHtml += "</table>";
strHtml += "</div>";
document.write(strHtml);
}//end printMainMenu()
//列印一級子功能表
function printSubMenu() {
for ( var i = 0 ; i< m_objMMList.length ; i ++ ) {
menuNode = m_objMMList[i];
//如果沒有子功能表,執行下一個迴圈
if ( !menuNode.hasChildNodes() ){
continue;
}
//得到主菜單下的子功能表集合
var objSMList = menuNode.childNodes;
var intSubMenuLen = objSMList.length;
//每個主菜單對應的子功能表id
var strSubMenuID = "sm" + i;
//arrMenuID.push(strSubMenuID);
var strSubMenuHtml = "<div style='display:none;border:0;' id='" + strSubMenuID + "'>" +
" <table cellspacing='0' cellpadding='4' style='" + m_strSubMenuStyle + "'>";
for ( var j = 0;j < intSubMenuLen;j ++ ) {
var objSubMenu = objSMList[j];
var attList = objSubMenu.attributes;
var menuText = attList.getNamedItem("text").text;
var menuHref = attList.getNamedItem("href").text;
var menuTarget = "";
if( attList.getNamedItem("target") )
menuTarget = attList.getNamedItem("target").text;
else
menuTarget = "_self";
//如果有三級子功能表,則需要箭頭標識
var strRightRow = " ";
//二級子功能表id
var strSubMenu2ID = strSubMenuID + "_sm" + j;
//滑鼠移至上方和移出時一級子功能表的響應
var strMouseOverEvent = "parent.chgSubMenuStyle(this,true);parent.endHideSubMenu();";
var strMouseOutEvent = " parent.chgSubMenuStyle(this,false);parent.beginHideSubMenu();";
//位置資訊顯示
var strPosition = STR_POS_FRONT + m_objMMList[i].attributes.getNamedItem("text").text + "->"
+ menuText;
//alert(strPosition);
if ( objSubMenu.hasChildNodes() ) {
//列印其二級子功能表
printSubMenu2(objSubMenu,strSubMenu2ID,strPosition);
strMouseOverEvent += "parent.showSubMenu2(\"" + strSubMenu2ID + "\",this);";
//strMouseOutEvent += "parent.beginHideSubMenu();";
strRightRow = "<font style='font-family:Webdings;font-size:7pt;height:10'>4</font>";
strPosition = "";
} else {
//strMouseOverEvent += "parent.endHideSubMenu();";
strMouseOverEvent += "parent.hideSubMenu2();";
}
var strOnClickEvent = "javascript:parent.onClickEvent('" + menuHref + "','" + menuTarget + "','" + strPosition + "');"
strSubMenuHtml += "<tr style='" + m_strSubMenuItemStyle + "' " +
" onmouseover='" + strMouseOverEvent + "' " +
" onmouseout='" + strMouseOutEvent + "' " +
" onclick= " + strOnClickEvent + " " +
" > <td width='3' nowrap style='" + m_strSMItemBorderB + "'> </td>"+
" <td nowrap style='" + m_strSMItemBorderB + "'>" + menuText + "</td>"+
" <td width='10' align='right' nowrap style='" + m_strSMItemBorderB + "'>" + strRightRow + "</td>" +
"</tr>";
//alert(objSubMenu.text.length);
}//end for 子功能表個數迴圈
strSubMenuHtml += "</table></div>";
document.write(strSubMenuHtml);
}//end for 主菜單個數迴圈
}//end function printSubMenu()
/**列印二級子功能表
*objSubMenu : 一級子功能表對象
*strSubMenu2ID :將要列印的二級菜單的id
*strPosition:主菜單和一級子功能表文本描述
*/
function printSubMenu2(objSubMenu,strSubMenu2ID,strPosition)
{
//arrMenuID.push(strSubMenu2ID);
var objSMList = objSubMenu.childNodes;
var intSubMenu2Len = objSMList.length;
var strSubMenu2Html = "<div style='display:none;border:0;' id='" + strSubMenu2ID + "'>" +
" <table cellspacing='0' cellpadding='4' style='" + m_strSubMenuStyle + "'>";
for ( var i = 0; i< intSubMenu2Len;i ++ )
{
var objSubMenu2 = objSMList[i];
var attList = objSubMenu2.attributes;
var menuText = attList.getNamedItem("text").text;
var menuHref = attList.getNamedItem("href").text;
var menuTarget = "";
if( attList.getNamedItem("target") )
menuTarget = attList.getNamedItem("target").text;
else
menuTarget = "_self";
var strPostion2 = strPosition + "->" + menuText;
strSubMenu2Html += "<tr style='" + m_strSubMenuItemStyle + "' " +
" onmouseover='parent.parent.chgSubMenuStyle(this,true);" +
" parent.parent.endHideSubMenu(); ' " +
" " +
" onclick=javascript:parent.parent.onClickEvent('" + menuHref + "','" + menuTarget + "','" + strPostion2 + "'); " +
" > <td width='3' nowrap style='" + m_strSMItemBorderB + "'> </td>"+
" <td nowrap style='" + m_strSMItemBorderB + "'>" + menuText + "</td>"+
" <td width='3' nowrap style='" + m_strSMItemBorderB + "'> </td>" +
"</tr>";
}
strSubMenu2Html += "</table></div>";
document.write(strSubMenu2Html);
}//end function printSubMenu2
/**
*開啟連結,所有菜單的連結都是通過這個函數來開啟。並顯示位置資訊
*@param strUrl - 開啟連結的地址
*@param strTarget - 開啟連結的target。如_self,_blank等
*@param strViewText - 位置資訊
*/
function onClickEvent(strUrl,strTarget,strViewText)
{
m_bolFirst = true;//以後響應菜單時變為為第一次響應
//document.all.selected_menu.innerText = strViewText;
m_strTitle = "";
var arrText = strViewText.split("->");
if ( arrText.length >0 )
m_strTitle = arrText[0];
for (var i = 1; i < arrText.length ; i ++ )
{
m_strTitle += "-" + arrText[i];
}
m_strTitle = m_strTitle.substring(STR_POS_FRONT.length);
//隱藏下拉式功能表
hideSubMenu();
//顯示title
if (m_strTitle != "")
{
//parent.document.title = m_strTitle;
}
if ( strUrl == "#" )
return;
//如果url是一個javascript字串則直接執行之
if(strUrl.toLowerCase().indexOf("javascript") != -1) {
executeScript(strUrl);
return;
}
strUrl = m_strContextPath + strUrl;//地址應加上虛擬目錄
window.open(strUrl,strTarget);
}
//執行一個javascript字串
function executeScript(strScript)
{
//第一個eval得到該字串表示的變數,第二個eval則執行變數裡的指令碼
eval(strScript);
}