1

我试图弄清楚为什么我无法通过键盘导航使用多个无序列表的菜单。

有没有人有任何提示?我觉得它比 html 更像是一个 CSS 问题。

相关的 CSS:

/* MENU */


#cssmenu{
    border:none;
    border:0px;
    margin:0px;
    padding:0px;
    font: 67.5% 'Lucida Sans Unicode', 'Bitstream Vera Sans', 'Trebuchet Unicode MS', 'Lucida Grande', Verdana, Helvetica, sans-serif;
    font-size:11px;
    font-weight:bold;   
    }
#cssmenu ul{
    background:#333333;
    height:39px;
    list-style:none;
    margin:0;
    padding:0;
    display: table;
    width: 100%;

    }
#cssmenu li{
    padding:0px;
    display: table-cell;
    width: 115px;
    }

#cssmenu li a{
    background:#333333 url('../images/seperator.gif') bottom right no-repeat;
    color:#FFF;
    display:block;
    font-weight:normal;
    line-height:39px;
    margin:0px;
    padding:0px 0px;
    text-align:center;
    text-decoration:none;
    width: 115px;

    }



#cssmenu li a:hover, #cssmenu ul li:hover a{
    background: #2580a2;
    color:#FFFFFF;
    text-decoration:none;
    float: none;
    }
#cssmenu li ul{
    background:#333333;
    display:none;
    height:auto;
    padding:0px;
    margin:0px;
    border:0px;
    position:absolute;
    width:230px;
    z-index:200;
    /*top:1em;
    /*left:0;*/
    }
#cssmenu li:hover ul{
    display:block;

    }
#cssmenu li li {
    background:url('../images/sub_sep.gif') bottom left no-repeat;
    display:block;
    float:none;
    margin:0px;
    padding:0px;
    width:230px;
    }
#cssmenu li:hover li a{
    background:none;
    }
#cssmenu li ul a{
    display:block;
    height:35px;
    font-size:10px;
    font-style:normal;
    margin:0px;
    padding:0px 10px 0px 15px;
    text-align:left;
    width: 205px;

    }
#cssmenu li ul a:hover, #cssmenu li ul li:hover a{
    background:#2580a2 url('../images/hover_sub.gif') center left no-repeat;
    border:0px;
    color:#ffffff;
    text-decoration:none;
    width: 205px;

    }
#cssmenu p{
    clear:left;
    }  

:focus {outline:none;}
::-moz-focus-inner {border:0;}

相关的 HTML 代码:

<div id="cssmenu">                                  <ul>                <li><a href="HTTP://wcmsdev/scripts/nc.dll?WCMS:HOMEPAGE:2059103169:pc=PC_90001">Home</a></li>          
                    <li class="has-sub">                <a href="HTTP://wcmsdev/scripts/nc.dll?WCMS:STANDARD:2059103169:pc=PC_90002">Operations</a>             <ul>            
                    <li><a href="HTTP://wcmsdev/scripts/nc.dll?WCMS:STANDARD:2059103169:pc=PC_90003">Individuals</a>                        
      </li>
                    <li><a href="HTTP://wcmsdev/scripts/nc.dll?WCMS:STANDARD:2059103169:pc=PC_90123">Aircraft owners</a>                        
      </li>
                    <li><a href="HTTP://wcmsdev/scripts/nc.dll?WCMS:STANDARD:2059103169:pc=PC_90276">Aircraft operators </a>                        
      </li>
                    <li><a href="HTTP://wcmsdev/scripts/nc.dll?WCMS:STANDARD:2059103169:pc=PC_90389">Flight training</a>                        
      </li>
                    <li><a href="HTTP://wcmsdev/scripts/nc.dll?WCMS:STANDARD:2059103169:pc=PC_90405">Aerodromes</a>                     
      </li>
                    <li><a href="HTTP://wcmsdev/scripts/nc.dll?WCMS:STANDARD:2059103169:pc=PC_90447">Office of airspace regulation</a>                      
      </li>
                    <li><a href="HTTP://wcmsdev/scripts/nc.dll?WCMS:STANDARD:2059103169:pc=PC_93439">Sport aviation</a>                     
      </li>
                    <li><a href="HTTP://wcmsdev/scripts/nc.dll?WCMS:STANDARD:2059103169:pc=PC_93379">Class D</a>                        
      </li>
                    <li><a href="HTTP://wcmsdev/scripts/nc.dll?WCMS:STANDARD:2059103169:pc=PC_100058">Non-towered aerodromes</a>                        
      </li>
                    <li><a href="HTTP://wcmsdev/scripts/nc.dll?WCMS:STANDARD:2059103169:pc=PC_90385">Dangerous goods</a>                        
      </li>
                    <li><a href="HTTP://wcmsdev/scripts/nc.dll?WCMS:STANDARD:2059103169:pc=PC_100808">Ground operations</a>                     
      </li>
                    <li><a href="HTTP://wcmsdev/scripts/nc.dll?WCMS:STANDARD:2059103169:pc=PC_100847"> Non-compliance notice </a>                       
      </li>
                    <li><a href="HTTP://wcmsdev/scripts/nc.dll?WCMS:STANDARD:2059103169:pc=PC_100374">Unmanned Aircraft Systems (UAS) </a>                      
      </li>
                    <li><a href="HTTP://wcmsdev/scripts/nc.dll?WCMS:STANDARD:2059103169:pc=PC_100705">General Aviation (GA) task force</a>                      
      </li>
                    <li><a href="HTTP://wcmsdev/scripts/nc.dll?WCMS:STANDARD:2059103169:pc=PC_100852">Regional Aviation Safety Forum (RASF)</a>                     
      </li>
                    <li><a href="http://casa.gov.au/aod" target="_top">Drug and Alcohol Management Plans</a>                        
      </li>
                    </ul></li>          
                    <li class="has-sub">                <a href="HTTP://wcmsdev/scripts/nc.dll?WCMS:STANDARD:2059103169:pc=PC_90473">Airworthiness</a>              <ul>            
                    <li><a href="HTTP://wcmsdev/scripts/nc.dll?WCMS:STANDARD:2059103169:pc=PC_100610">Maintenance regulations</a>                       
      </li>
                    <li><a href="HTTP://wcmsdev/scripts/nc.dll?WCMS:STANDARD:2059103169:pc=PC_90474">Personnel</a>                      
      </li>
                    <li><a href="HTTP://wcmsdev/scripts/nc.dll?WCMS:STANDARD:2059103169:pc=PC_90496">Certification and design</a>                       
      </li>
                    <li><a href="HTTP://wcmsdev/scripts/nc.dll?WCMS:STANDARD:2059103169:pc=PC_90507">Manufacturing</a>                      
      </li>
                    <li><a href="HTTP://wcmsdev/scripts/nc.dll?WCMS:STANDARD:2059103169:pc=PC_90515">Continuing airworthiness</a>                       
      </li>
                    <li><a href="HTTP://wcmsdev/scripts/nc.dll?WCMS:STANDARD:2059103169:pc=PC_90822">Airworthiness directives</a>                       
      </li>
                    <li><a href="HTTP://wcmsdev/scripts/nc.dll?WCMS:STANDARD:2059103169:pc=PC_90821">Maintenance organisations</a>                      
      </li>
                    <li><a href="HTTP://wcmsdev/scripts/nc.dll?WCMS:STANDARD:2059103169:pc=PC_101038">Flight test and evaluation</a>                        
      </li>
                    </ul></li>          
                    <li class="has-sub">                <a href="HTTP://wcmsdev/scripts/nc.dll?WCMS:STANDARD:2059103169:pc=PC_90900">Regulations &amp; policy</a>               <ul>            
                    <li><a href="HTTP://wcmsdev/scripts/nc.dll?WCMS:STANDARD:2059103169:pc=PC_90902">Current rules</a>                      
      </li>
                    <li><a href="HTTP://wcmsdev/scripts/nc.dll?WCMS:PWA:2059103169:pc=PC_91070">Changing the rules</a>                      
      </li>
                    <li><a href="HTTP://wcmsdev/scripts/nc.dll?WCMS:STANDARD:2059103169:pc=PC_91181">Enforcement action</a>                     
      </li>
                    <li><a href="HTTP://wcmsdev/scripts/nc.dll?WCMS:PWA:2059103169:pc=PC_91190">Policy notices</a>                      
      </li>
                    <li><a href="HTTP://wcmsdev/scripts/nc.dll?WCMS:STANDARD:2059103169:pc=PC_100396">Australia's state safety program</a>                      
      </li>
                    <li><a href="HTTP://wcmsdev/scripts/nc.dll?WCMS:STANDARD:2059103169:pc=PC_101078">Performance based navigation (PBN)</a>                        
      </li>
                    <li><a href="HTTP://wcmsdev/scripts/nc.dll?WCMS:STANDARD:2059103169:pc=PC_101326">Licensing regulations</a>                     
      </li>
                    </ul></li>          
                    <li class="has-sub">                <a href="HTTP://wcmsdev/scripts/nc.dll?WCMS:STANDARD:2059103169:pc=PC_91263">Manuals &amp; forms</a>                <ul>            
                    <li><a href="HTTP://wcmsdev/scripts/nc.dll?WCMS:STANDARD:2059103169:pc=PC_91264">Manuals</a>                        
      </li>
                    <li><a href="HTTP://wcmsdev/scripts/nc.dll?WCMS:STANDARD:2059103169:pc=PC_91308">Forms</a>                      
      </li>
                    <li><a href="HTTP://wcmsdev/scripts/nc.dll?WCMS:STANDARD:2059103169:pc=PC_91316">CASA online store</a>                      
      </li>
                    </ul></li>          
                    <li class="has-sub">                <a href="HTTP://wcmsdev/scripts/nc.dll?WCMS:STANDARD:2059103169:pc=PC_91314">Education</a>              <ul>            
                    <li><a href="HTTP://wcmsdev/scripts/nc.dll?WCMS:STANDARD:2059103169:pc=PC_101349">AviationWorx</a>                      
      </li>
                    <li><a href="HTTP://wcmsdev/scripts/nc.dll?WCMS:STANDARD:2059103169:pc=PC_100101">eLearning catalogue</a>                       
      </li>
                    <li><a href="HTTP://wcmsdev/scripts/nc.dll?WCMS:STANDARD:2059103169:pc=PC_91346">Flight Safety Australia</a>                        
      </li>
                    <li><a href="HTTP://wcmsdev/scripts/nc.dll?WCMS:STANDARD:2059103169:pc=PC_90006"> Pilot guides and information </a>                     
      </li>
                    <li><a href="HTTP://wcmsdev/scripts/nc.dll?WCMS:STANDARD:2059103169:pc=PC_100138">OnTrack</a>                       
      </li>
                    <li><a href="HTTP://wcmsdev/scripts/nc.dll?WCMS:STANDARD:2059103169:pc=PC_100994">Human factors</a>                     
      </li>
                    <li><a href="HTTP://wcmsdev/scripts/nc.dll?WCMS:STANDARD:2059103169:pc=PC_91430">Safety management systems</a>                      
      </li>
                    <li><a href="HTTP://wcmsdev/scripts/nc.dll?WCMS:STANDARD:2059103169:pc=PC_91329">Seminars and workshops</a>                     
      </li>
                    <li><a href="HTTP://wcmsdev/scripts/nc.dll?WCMS:STANDARD:2059103169:pc=PC_91317">Aviation safety advisers</a>                       
      </li>
                    <li><a href="HTTP://wcmsdev/scripts/nc.dll?WCMS:STANDARD:2059103169:pc=PC_91455">Advice for air travellers</a>                      
      </li>
                    <li><a href="HTTP://wcmsdev/scripts/nc.dll?WCMS:STANDARD:2059103169:pc=PC_91316">CASA online store</a>                      
      </li>
                    <li><a href="HTTP://wcmsdev/scripts/nc.dll?WCMS:STANDARD:2059103169:pc=PC_101317">Out-n-Back</a>                        
      </li>
                    </ul></li>          
                    <li class="has-sub">                <a href="HTTP://wcmsdev/scripts/nc.dll?WCMS:STANDARD:2059103169:pc=PC_91488">Services</a>               <ul>            
                    <li><a href="HTTP://wcmsdev/scripts/nc.dll?WCMS:STANDARD:2059103169:pc=PC_91489">Licences and registrations</a>                     
      </li>
                    <li><a href="HTTP://wcmsdev/scripts/nc.dll?WCMS:STANDARD:2059103169:pc=PC_91510">Service standards and fees</a>                     
      </li>
                    <li><a href="HTTP://wcmsdev/scripts/nc.dll?WCMS:PWA:2059103169:pc=PC_91499">Self Service Portal</a>                     
      </li>
                    <li><a href="HTTP://wcmsdev/scripts/nc.dll?WCMS:STANDARD:2059103169:pc=PC_91517">Permission application centre (PAC)</a>                        
      </li>
                    <li><a href="HTTP://wcmsdev/scripts/nc.dll?WCMS:STANDARD:2059103169:pc=PC_91521">Aviation medicals</a>                      
      </li>
                    <li><a href="HTTP://wcmsdev/scripts/nc.dll?WCMS:STANDARD:2059103169:pc=PC_100450">Delegates and authorised persons</a>                      
      </li>
                    </ul></li>          
                    <li class="has-sub">                <a href="HTTP://wcmsdev/scripts/nc.dll?WCMS:STANDARD:2059103169:pc=PC_91621">About CASA</a>             <ul>            
                    <li><a href="HTTP://wcmsdev/scripts/nc.dll?WCMS:STANDARD:2059103169:pc=PC_91622">Contact CASA</a>                       
      </li>
                    <li><a href="HTTP://wcmsdev/scripts/nc.dll?WCMS:STANDARD:2059103169:pc=PC_93342">CASA board</a>                     
      </li>
                    <li><a href="HTTP://wcmsdev/scripts/nc.dll?WCMS:STANDARD:2059103169:pc=PC_91668">CASA's Director</a>                        
      </li>
                    <li><a href="HTTP://wcmsdev/scripts/nc.dll?WCMS:STANDARD:2059103169:pc=PC_91703">Recent media releases </a>                     
      </li>
                    <li><a href="HTTP://wcmsdev/scripts/nc.dll?WCMS:STANDARD:2059103169:pc=PC_92568">Corporate publications</a>                     
      </li>
                    <li><a href="HTTP://wcmsdev/scripts/nc.dll?WCMS:STANDARD:2059103169:pc=PC_100448">CASA on Twitter</a>                       
      </li>
                    <li><a href="HTTP://wcmsdev/scripts/nc.dll?WCMS:STANDARD:2059103169:pc=PC_92918">Corporate policy</a>                       
      </li>
                    <li><a href="HTTP://wcmsdev/scripts/nc.dll?WCMS:STANDARD:2059103169:pc=PC_92936">Careers at CASA</a>                        
      </li>
                    <li><a href="HTTP://wcmsdev/scripts/nc.dll?WCMS:STANDARD:2059103169:pc=PC_92956">Research and statistics</a>                        
      </li>
                    <li><a href="HTTP://wcmsdev/scripts/nc.dll?WCMS:STANDARD:2059103169:pc=PC_100223">International Engagement</a>                      
      </li>
                    <li><a href="HTTP://wcmsdev/scripts/nc.dll?WCMS:STANDARD:2059103169:pc=PC_100330">CASA mailing lists</a>                        
      </li>
                    </ul></li>          
</ul>
</div>
4

2 回答 2

2

它不起作用,因为您只有:hover键盘不会触发的规则。当一个元素具有选项卡焦点时,它具有:focus伪类,当您添加它时,您会注意到您的菜单项在获得焦点时会亮起,但仍然不会打开菜单。

jsFiddle:与:focus边规则一起使用:hover规则

在此处输入图像描述

这是由于以下规则:

#cssmenu li:hover ul,
#cssmenu li:focus ul{
    display:block;
}

从来没有真正获得焦点,li因为它没有标签索引,a内部li获得焦点。由于li是父级,ul但又a是兄弟级,ul我们需要使用不同的规则来使此定位起作用。我们可以使用下一个兄弟选择器+来定位ulbased on a:focus。这让我们更近了一步。

#cssmenu li:hover ul,
#cssmenu li a:focus + ul{
    display:block;
}   

jsFiddle 应用了上述内容

在此处输入图像描述

不幸的是,这是您使用 CSS 所能做到的(据我所知)。当我们尝试进入下一个a时,:focus将被丢弃在顶层a并且菜单将隐藏,因为子菜单将丢失display:block;。由于在应用样式时我们无法向上遍历树,因此当我们进一步向下时,我们无法将其应用display:block到祖先。ul:focusa

我能看到解决这个问题的唯一方法是使用 JavaScript 并处理顶级菜单项的focus(也许是 )事件。blur()然后,我们还必须处理click()事件以在单击文档中的任何位置时隐藏菜单,否则它将保持活动状态。

jsFiddle完全工作

在此处输入图像描述

CSS

#cssmenu li.force-show ul,
#cssmenu li:hover ul,
#cssmenu li a:focus + ul{
    display:block;
}

JS

$('#cssmenu > ul > li > a').focus(function () {
    $('#cssmenu > ul > li').removeClass('force-show');
    $(this).parent().addClass('force-show');
});

// Clear the menu class if clicked anywhere in the document.
$(document).click(function () {
    $('#cssmenu > ul > li').removeClass('force-show');
});
于 2013-04-05T02:47:12.953 回答
0

问题是,focus在 CSS 中不会冒泡。我最近来的是这个

/* for instance use it that way */
#cssmenu li a:hover,
#cssmenu li a:focus, 
#cssmenu ul li:hover a, 
#cssmenu ul li:focus a { }

该示例适用于第一级。但是在去第二个的时候,第一个失去了它:focus,所以子菜单又被隐藏了。我认为您需要对 JavaScript 有所帮助。

于 2013-04-05T02:34:47.807 回答