HTML - 尝试选择嵌套选择标记时关闭下拉菜单

时间:2015-04-14 11:08:42

标签: javascript jquery html drop-down-menu html-select

错误地标记为重复

JS小提琴(问题)

http://jsfiddle.net/xrd2b42L/12/

JS小提琴(有解决方案)

http://jsfiddle.net/xrd2b42L/21/

问题

我有一个由JQuery提供支持的下拉菜单。如果我在菜单中放置了一个选择标签,并尝试选择一个选项,菜单会自动关闭,而我无法选择一个选项。

此错误不是跨浏览器的一致性。此菜单在Firefox 37.01,Safari 5.1.7中正常运行,也可在Chrome 39中使用。

我已尝试将启动菜单的JavaScript事件更改为点击事件,但仍会出现错误。

该错误发生在IE11和Chrome 41.0.2272.118中。

将不胜感激。

更新

我已经更新了使用mouseenter事件的小提琴。问题仍然存在。 Kano指出,当我将鼠标悬停在选择内的选项上时,问题似乎是触发鼠标离开事件。

修正

经过4个小时的努力,我设法解决了这个问题。事实证明,即使select元素是处理程序附加到的元素的子元素,也会在select元素上触发鼠标离开事件。我已修复下面的javascript,以便代码现在可以正常运行,以防其他人遇到此问题。

HTML

<html>
<header>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
</header>
<body>
<ul class = 'contentMenu jsGeneralNavigation'>
<li>Menu Item 1</li>
<li>Drop Down Menu 1
<ul>
<li>No Select</li>
<li>
<select>
<option value ='Option 1'>Option 1</option>
<option value ='Option 2'>Option 2</option>
<option value ='Option 3'>Option 3</option>    
</select>
<li>No Select</li>
</ul>
</ul>
</body>
</html>

CSS

/******************************************************
* CSS Reset
* http://meyerweb.com/eric/tools/css/reset/ 
* v2.0 | 20110126
******************************************************/
html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed, 
figure, figcaption, footer, header, hgroup, 
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
    margin: 0;
    padding: 0;
    border: 0;
    font-size: 100%;
    font: inherit;
    vertical-align: baseline;
}

/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure, 
footer, header, hgroup, menu, nav, section {
    display: block;
}

body {
    line-height: 1;
}

ol, ul {
    list-style: none;
}

blockquote, q {
    quotes: none;
}

blockquote:before, blockquote:after,
q:before, q:after {
    content: '';
    content: none;
}

table {
    border-collapse: collapse;
    border-spacing: 0;
}
/*************************************************************
* End CSS Reset
*************************************************************/

ul.contentMenu li {
    float: left;
    position: relative;    
    list-style: none;
    text-align: center;    
    color: #000;
    cursor: default;
    display: block;
    font-size: 12px;
    text-decoration: none;
    padding:5px;
    border-radius: 3px 3px 0px 0px;
    border: 1px #ddd solid;
    border-bottom:0px;
    background-color: #F0F0F0;
    margin-right: 5px;
}

ul.contentMenu li.pesudoClassHover  {
    background-color: #ddd;
}

ul.contentMenu ul {
    z-index: 10;
    display: none;
    position: absolute;
    top: 22px;
    left: -1px;
    box-shadow: 5px 5px 5px #aaa;
    border-top:1px solid #ddd;
    border-top-right-radius: 3px;  
    border-bottom-left-radius: 3px;
    border-bottom-right-radius: 3px;    
    background-color: #fff; /* Important fixes Ie.7. bug where relatively positioned element overlaps absolutely positioned element */
}

/* Keep this seperate and above first child declaration otherwise it breaks I.e. 8 */
ul.contentMenu ul li {
    border: none; /* Important - Reset border */
    border-radius: 0px; /* Important - Reset border */
    padding:5px;
    margin: 0px;
    float: none;
    text-align: left;
    text-shadow: none;
    background-color: #fff;
    border-left:1px solid #ddd;
    border-right:1px solid #ddd;
    border-bottom:1px solid #ddd;
}

ul.contentMenu ul li a {
    display: block;
    border: none;
    text-decoration: none;
    color:#000;
    padding:5px;
    font-size: 12px;
}

ul.contentMenu li ul li:hover { 
    background-color: #ddd;
}

ul.contentMenu ul li:last-child {
    border-bottom-left-radius: 3px;
    border-bottom-right-radius: 3px;
}

ul.contentMenu li ul li a:hover {
    background-color: #ddd;
}

JavaScript(使用解决方案)

$(document).ready(function() 
{
        //Javascript for desktop navigation
        $('ul.jsGeneralNavigation > li').on('mouseenter', function() 
        {                   
            $(this).addClass('pesudoClassHover');
            $(this).children('ul').show();
        });   

        //Javascript for desktop navigation
        $('ul.jsGeneralNavigation > li').on('mouseleave', function(e) 
        {
            //This is the fix to prevent the select triggering the mouse leave
            if(e.target.tagName.toLowerCase() != "select") {
                $(this).removeClass('pesudoClassHover');
                $(this).children('ul').hide();
            }
        });   
});

0 个答案:

没有答案