带子菜单的下拉菜单

时间:2012-07-12 20:19:57

标签: javascript html drop-down-menu menu

我在导航中插入另一个新子菜单时遇到问题。

这是我的Javscript代码:

// Main Nav Javascript Function *****************************************
var navTimer = "";
bop = new Object();
bop.targetDiv = "";
bop.hide = function(){
document.getElementById(bop.targetDiv).style.visibility = "hidden";
}

function openNav(myDiv){
if(bop.targetDiv != "") bop.hide();
    bop.targetDiv = myDiv;
    clearTimeout(navTimer);
    document.getElementById(myDiv).style.visibility = "visible";
    document.getElementById(myDiv).onmouseout = function(){
        navTimer = setTimeout("bop.hide()", 100);
    }
    document.getElementById(myDiv).onmouseover = function(){
        clearTimeout(navTimer);
    }
}

function hideNav(myDiv){
    bop.targetDiv = myDiv;
    navTimer = setTimeout("bop.hide()", 150);
}

<!-- Display Sub Nav / Clear Sub Nav -->
function SubNav(myVar){
document.getElementById(myVar).style.visibility = "visible";
}

function clearNav(myVar){
document.getElementById(myVar).style.visibility = "hidden";
}

function shortPopUp( url, name, width, height, scrollbars ) { 
    var top  = "0"; 
    var left = "0"; 

    if( scrollbars == null ) scrollbars = "0"; 

    str  = ""; 
    str += "resizable=1,titlebar=1,menubar=1,"; 
    str += "toolbar=0,location=0,directories=0,status=0,"; 
    str += "scrollbars=1" + scrollbars + ","; 
    str += "width=" + width + ","; 
    str += "height=" + height + ","; 
    str += "top=" + top + ","; 
    str += "left=" + left; 

    window.open( url, name, str ); 
}

这是我的HTML代码:

><div class="navItem" onMouseOver="SubNav('seat-twenty');" onMouseOut="clearNav('seat-twenty');"><a href="fps_twenty2.html" target="frame">Twenty2</a><div id='seat-twenty' class="dropDown" onMouseOver="SubNav('seat-twenty');" onMouseOut="clearNav('seat-twenty');">
<div class="navItem"><a href="fps_twenty2.html" target="frame">Twenty2 Overview</a></div>
<div class="navItem"><a href="fb_twenty2_black.html" target="frame">Twenty2</a></div>
<div class="navItem"><a href="fb_twenty2_red.html" target="frame">Twenty2-US</a></div>
<div class="navItem"><a href="fb_twenty2_red_black.html" target="frame">Twenty2-UP</a></div>
</div></div>

可以找到服务器上的页面here

问题出在导航的座位标签下,当我尝试插入新标签“Twenty2”,而不是打开一个额外的子菜单时,子菜单仍然保留在同一个下拉菜单中......我检查了代码一遍又一遍,我的错误代码和正确的代码没有区别。

以下是可行的代码:

> <div class="navItem" onMouseOver="SubNav('seat-heavy');"
> onMouseOut="clearNav('seat-heavy');"><a href="fps_heavy_duty.html"
> target="frame">Heavy Duty</a><div id='seat-heavy' class="dropDown"
> onMouseOver="SubNav('seat-heavy');"
> onMouseOut="clearNav('seat-heavy');">

有人知道什么是错的吗?

2 个答案:

答案 0 :(得分:1)

您的工作菜单项具有相关的样式表规则,例如

#seat-healthcare {
    background-color: #E7E7E7;
    position: absolute;
    margin-top: -18px;
    margin-left: 130px;
    z-index: 199;
    width: 119px;
    padding-top: 3px;
    padding-bottom: 4px;
}

但你没有#seat-twenty,所以当它弹出时会立即消失,因为它认为你滚动了因为它没有左边缘设置。

总而言之,所有菜单都以愚蠢的方式运作,我真的建议您使用更通用的东西,这样您就不必为每个项目都设置样式表条目。

插入样式表样式表的相关JS

// Browser & Platform Detection & Style Sheet Documentation *******************
if(navigator.userAgent.indexOf("Safari")!=-1){
    document.write("<link href='styles/MacSafari_style.css' rel='stylesheet' type='text/css'>");

}
if(navigator.userAgent.indexOf("Firefox")!=-1){
    if(navigator.appVersion.indexOf("Mac")!=-1){
    // Added Fix for High Rez Screens
    if((screen.width >= 1400) && (screen.height >= 1050)){
        document.write("<link href='styles/HighRez_MacMozilla_style.css' rel='stylesheet' type='text/css'>");
    } else {
        document.write("<link href='styles/MacMozilla_style.css' rel='stylesheet' type='text/css'>");
    }
} else {
    document.write("<link href='styles/Mozilla_style.css' rel='stylesheet' type='text/css'>");
    }
}
if(navigator.appName == "Microsoft Internet Explorer"){
    if(navigator.appVersion.indexOf("Mac")!=-1){ 
        document.write("<link href='styles/MacIE_style.css' rel='stylesheet' type='text/css'>");
    } else {
    document.write("<link href='styles/IE_style.css' rel='stylesheet' type='text/css'>");
}
}

答案 1 :(得分:1)

你那里有太多的div。检查其他工作子导航菜单之一。它只是一个带有链接的单个div。

例如,这是表格菜单中的一个片段:

会议

我也同意另一张海报;看看如何制作CSS菜单。特别是,http://www.htmldog.com/articles/suckerfish/dropdowns/是一篇很棒的文章,只要你不需要IE6支持,你就不需要使用Javascript。