想要隐藏身体上的子菜单

时间:2013-06-11 11:43:49

标签: javascript jquery

我想隐藏我的子菜单点击身体。目前它不是这样工作,它只在单击下拉菜单项时隐藏。这是菜单所在的(https://deva.yuave.com)网站。 我试图隐藏它在身体点击,但它不会工作。 Plz帮助..这是我用于子菜单的javascript代码..

<script type="text/javascript">
<!--

window.onload=function() {
 subMenusO = getElementsByClassName(document,'ul','subMenus');
}
//-----------------------------------------------------------

function showSubMenu(elemId) {
 var dispStatus = (document.getElementById(elemId).style.display == 'block')? 'none' : 'block';
 //hide any visible submenus
 for(var i=0; i < subMenusO.length; i=i+1) {
     subMenusO[i].style.display = 'none';
    }
    //show/hide this menu id
    document.getElementById(elemId).style.display = dispStatus;
}
//--------------------------------------------------------------------------

function getElementsByClassName(oElm, strTagName, strClassName){
    var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
    var arrReturnElements = new Array();
    strClassName = strClassName.replace(/\-/g, "\\-");
    var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
    var oElement;
    for(var i=0; i<arrElements.length; i++)
    {
        oElement = arrElements[i];
        if(oRegExp.test(oElement.className))
        {
            arrReturnElements.push(oElement);
        }
    }
    return (arrReturnElements)
}

//-->
</script>

1 个答案:

答案 0 :(得分:2)

像这样的东西

$(document).click(function(e) {
    if (!$(e.target).is(".subMenus")){
        // hide submenus
        $('.subMenus').hide();
    }
});