2

我想在身体点击时隐藏我的子菜单。目前它不是这样工作的,它只在单击下拉菜单项时隐藏。这是菜单所在的 ( https://deva.yuave.com ) 站点。我试图在身体点击时隐藏它,但它不起作用。请帮助..这是我用于子菜单的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>
4

1 回答 1

2

像这样的东西

$(document).click(function(e) {
    if (!$(e.target).is(".subMenus")){
        // hide submenus
        $('.subMenus').hide();
    }
});
于 2013-06-11T11:52:54.843 回答