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);
}

三、典型的xml配置
<?xml version="1.0" encoding="GB2312"?>

<popmenu>
    <!--样式定义-->
    <style>
        <!--主菜单样式定义-->
        <main-menu>
            <!--基本样式,只要节点符合table style attribute即可-->
            <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>
            <!--菜单项的样式-->
            <menu-item>
                <!--基本样式,节点符合td style attribute即可,可选。鼠标移出时使用该样式-->
                <base>
                    <background-color></background-color>
                    <color>#000099</color>
                </base>
                <!--鼠标悬停时的样式,节点符合td style attribute即可,可选-->
                <mouse-over>
                    <background-color>white</background-color>
                    <border>1 solid #6699CC</border>
                    <color>green</color>
                </mouse-over>
                <!--菜单项与项之间的分割图片,可选-->
                <split-image>url(menu_split.gif)</split-image>
            </menu-item>
        </main-menu>

        <!--子菜单样式定义-->
        <sub-menu>
            <!--基本样式,只要节点符合table style attribute即可-->
            <base>
                <font-size>9pt</font-size>
                <border>1 solid black</border>
                <cursor>default</cursor>
            </base>
            <!--菜单项的样式-->
            <menu-item>
                <!--基本样式,节点符合tr style attribute即可,可选。鼠标移出时使用该样式-->
                <base>
                    <background-color>white</background-color>
                    <color>#006699</color>
                </base>
                <!--鼠标悬停时的样式,节点符合tr style attribute即可,可选。-->
                <mouse-over>
                    <background-color>#DDDDDD</background-color>
                    <color>#000066</color>
                </mouse-over>
                <!--菜单项与项之间的分割线,参数符合border样式即可。可选-->
                <split-line>1 solid #999999</split-line>
            </menu-item>
        </sub-menu>
    </style>

    <!--
        context path相当于虚拟目录的别名。如果菜单的链接使用绝对路径访问,则建议设置该节点。
        如果设置该节点则菜单的链接地址由该值加上菜单项设置的href构成。
        该节点是可选的
    -->
    <context>
        <path></path>
    </context>

    <!--
        菜单数据。
        menus下的节点视为主菜单,只有主菜单设置的宽度值才有效
        菜单必须有text,href两个属性,target为可选属性,默认值为"_self"
            text - 菜单文本
            href - 菜单链接地址。如果不需链接请用"#";如果是执行某段javascript,请加上前缀javascript:
            target - 菜单打开的窗口
    -->
    <menus>
        <menu text="CSDN" href="#" target="_blank" width="60">
            <menu text="网站首页" href="http://www.csdn.net" target="_blank">
            </menu>
            <menu text="服务" href="#">
                <menu text="blog" href="'>http://blog.csdn.net/"></menu>
                <menu text="技术社区" href="'>http://community.csdn.net/"></menu>
                <menu text="帮助" href="javascript:help();"></menu>
            </menu>
        </menu>
        <menu text="SOHU" href="http://www.sohu.com" target="_blank" width="60">
        </menu>
        <menu text="NCUCEC" href="http://www.infocespace.cn" target="_blank" width="60">
            <menu text="公司网站" href="http://www.infospace.cn" target="_blank"></menu>
            <menu text="内部论坛" href="http://www.infospace.cn/forums" target="_self"></menu>
        </menu>
    </menus>
</popmenu>

四、使用例子

<html>
<head>
<title>javascript+xml二级下拉菜单(使用window.createPopup())</title>

</head>
<body bgcolor="#cccccc">

<script language="javascript" src="menu_script1.js"></script>
<script language="javascript" src="menu_script2.js"></script>

<div>
    <script language="javascript">loadMenu("menu_data.xml");</script>
</div>

</body>
</html>

只要节点名称符合各自标签(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);
}

三、典型的xml配置
<?xml version="1.0" encoding="GB2312"?>

<popmenu>
    <!--样式定义-->
    <style>
        <!--主菜单样式定义-->
        <main-menu>
            <!--基本样式,只要节点符合table style attribute即可-->
            <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>
            <!--菜单项的样式-->
            <menu-item>
                <!--基本样式,节点符合td style attribute即可,可选。鼠标移出时使用该样式-->
                <base>
                    <background-color></background-color>
                    <color>#000099</color>
                </base>
                <!--鼠标悬停时的样式,节点符合td style attribute即可,可选-->
                <mouse-over>
                    <background-color>white</background-color>
                    <border>1 solid #6699CC</border>
                    <color>green</color>
                </mouse-over>
                <!--菜单项与项之间的分割图片,可选-->
                <split-image>url(menu_split.gif)</split-image>
            </menu-item>
        </main-menu>

        <!--子菜单样式定义-->
        <sub-menu>
            <!--基本样式,只要节点符合table style attribute即可-->
            <base>
                <font-size>9pt</font-size>
                <border>1 solid black</border>
                <cursor>default</cursor>
            </base>
            <!--菜单项的样式-->
            <menu-item>
                <!--基本样式,节点符合tr style attribute即可,可选。鼠标移出时使用该样式-->
                <base>
                    <background-color>white</background-color>
                    <color>#006699</color>
                </base>
                <!--鼠标悬停时的样式,节点符合tr style attribute即可,可选。-->
                <mouse-over>
                    <background-color>#DDDDDD</background-color>
                    <color>#000066</color>
                </mouse-over>
                <!--菜单项与项之间的分割线,参数符合border样式即可。可选-->
                <split-line>1 solid #999999</split-line>
            </menu-item>
        </sub-menu>
    </style>

    <!--
        context path相当于虚拟目录的别名。如果菜单的链接使用绝对路径访问,则建议设置该节点。
        如果设置该节点则菜单的链接地址由该值加上菜单项设置的href构成。
        该节点是可选的
    -->
    <context>
        <path></path>
    </context>

    <!--
        菜单数据。
        menus下的节点视为主菜单,只有主菜单设置的宽度值才有效
        菜单必须有text,href两个属性,target为可选属性,默认值为"_self"
            text - 菜单文本
            href - 菜单链接地址。如果不需链接请用"#";如果是执行某段javascript,请加上前缀javascript:
            target - 菜单打开的窗口
    -->
    <menus>
        <menu text="CSDN" href="#" target="_blank" width="60">
            <menu text="网站首页" href="http://www.csdn.net" target="_blank">
            </menu>
            <menu text="服务" href="#">
                <menu text="blog" href="'>http://blog.csdn.net/"></menu>
                <menu text="技术社区" href="'>http://community.csdn.net/"></menu>
                <menu text="帮助" href="javascript:help();"></menu>
            </menu>
        </menu>
        <menu text="SOHU" href="http://www.sohu.com" target="_blank" width="60">
        </menu>
        <menu text="NCUCEC" href="http://www.infocespace.cn" target="_blank" width="60">
            <menu text="公司网站" href="http://www.infospace.cn" target="_blank"></menu>
            <menu text="内部论坛" href="http://www.infospace.cn/forums" target="_self"></menu>
        </menu>
    </menus>
</popmenu>

四、使用例子

<html>
<head>
<title>javascript+xml二级下拉菜单(使用window.createPopup())</title>

</head>
<body bgcolor="#cccccc">

<script language="javascript" src="menu_script1.js"></script>
<script language="javascript" src="menu_script2.js"></script>

<div>
    <script language="javascript">loadMenu("menu_data.xml");</script>
</div>

</body>
</html>

时间: 2024-11-01 19:00:26

javascript+xml实现二级下拉菜单,不会被任何标签或元素遮住的相关文章

&amp;#106avascript+xml实现二级下拉菜单,不会被任何标签或元素遮住

xml|菜单|下拉 一.简单说明 该菜单最大的优点是可以跨过网页上任何标签,甚至是框架,(一般的菜单会被select,object,flash等挡住)所以如果你有这方面的需要,那么这个菜单是个很好的选择. 总体而言,它有如下特性:(1)菜单的样式和数据都是通过xml配置(2)菜单的样式可以随心所欲修改,如: <base>     <background-color>#DDDDDD</background-color>     <background-image&g

javascript+xml实现二级下拉菜单一

javascript|xml|菜单|下拉 一.简单说明 该菜单最大的优点是可以跨过网页上任何标签,甚至是框架,(一般的菜单会被select,object,flash等挡住)所以如果你有这方面的需要,那么这个菜单是个很好的选择. 总体而言,它有如下特性: (1)菜单的样式和数据都是通过xml配置 (2)菜单的样式可以随心所欲修改,如: <base><background-color>#DDDDDD</background-color><background-imag

Javascript实现简单二级下拉菜单实例_javascript技巧

复制代码 代码如下: <span style="font-size:14px;"><!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&

基于jQuery实现二级下拉菜单效果_jquery

本文通过代码实例详细介绍一下简单的二级下拉菜单是如何实现的,当然还有更为复杂的二级菜单,不过先学会如何制作简单的,分享给大家供大家参考,具体内容如下 代码如下: <html> <head> <meta charset=" utf-8"> <title>下拉菜单</title> <style type="text/css"> nav a{ text-decoration:none; } nav&g

jQuery实现二级下拉菜单效果_jquery

大家都知道jQuery是一个框架,它对JS进行了封装,使其更方便使用.前面两篇博文分别是用CSS样式和JS实现的,那么这篇就用jQuery来实现二级下拉式菜单. 使用JQuery实现需要用到的知识有:        1)使用$(function(){...})获取到想要作用的HTML元素.        2)通过使用children()方法寻找子元素.        3)通过使用show()方法来显示HTML元素.        4)通过使用hide()方法来隐藏HTML元素.        5

jQuery实现简单二级下拉菜单_jquery

html代码 <div class="UpLayer"> <dl> <dt> <a href="javascript:void(0)"></a></dt> <dd> <a href="http://www.demo.com/js/">特效</a> <a href="http://www.demo.com/Tutorials/

JS 实现导航菜单中的二级下拉菜单的几种方式_javascript技巧

最近整理了, JS 实现导航菜单中的二级下拉菜单的三种方式,便于项目中应用.  如何实现导航菜单栏中的二级下拉菜单? 我们在淘宝.搜狐等大型网站上都可以看到使用的一些二级下拉菜单,比如下面这张图片. 但是如何实现类似的图片呢?实际上,我们有至少三种方式来实现,下面,我附上代码供大家参考. 1.仅使用html和css <meta charset="UTF-8"> <title>Document</title> <style> *{margi

JS中用三种方式实现导航菜单中的二级下拉菜单_javascript技巧

我们在淘宝.搜狐等大型网站上都可以看到使用的一些二级下拉菜单,比如下面这张图片.那么如何实现导航菜单栏中的二级下拉菜单?下面小编给大家分享实现思路. 但是如何实现类似的图片呢?实际上,我们有至少三种方式来实现,下面,我附上代码供大家参考. 1.仅使用html和css <meta charset="UTF-8"> <title>Document</title> <style> *{margin:0;padding: 0;list-style

jquery实现漂亮的二级下拉菜单代码_jquery

本文实例讲述了jquery实现漂亮的二级下拉菜单代码.分享给大家供大家参考.具体如下: 这里介绍一款基于jquery实现的网站下拉菜单,黑色风格,很漂亮,本菜单需要点击主菜单后的小三角符号才下拉出二级菜单,并不是有些菜单,是鼠标移上主菜单的时候就滑过,至于哪一种,就看个人的喜好了 先来看看运行效果: 在线演示地址如下: http://demo.jb51.net/js/2015/jquery-bg-2-level-down-show-menu-codes/ 具体代码如下: <!DOCTYPE ht