简体   繁体   English

jQuery Dropdown菜单在IE和Webkit中的行为有所不同

[英]jQuery Dropdown menu behaves different in IE and Webkit

The desired behavior is: 所需的行为是:

  • click on parent to open dropdown menu 单击父级以打开下拉菜单
  • click anywhere on page or on parent, to close dropdown menu 单击页面或父级上的任意位置以关闭下拉菜单

My jQuery code works fine in Safari & Firefox, but not in IE 8. In IE 8 the dropdown menu opens and closes again when the parent is clicked for the first time. 我的jQuery代码在Safari和Firefox中工作正常,但在IE 8中却无法正常工作。在IE 8中,第一次单击父级时,下拉菜单会打开并再次关闭。 The second time the parent is clicked the dropdown menu stays open. 第二次单击父级时,下拉菜单保持打开状态。 I am using jQuery 1.6.2. 我正在使用jQuery 1.6.2。

Here's the jQuery code (a bit of a Frankenstein I have cobbled together from different methods I read about): 这是jQuery代码(我从阅读过的不同方法中拼凑了一些科学怪人的名字):

$(function() {
/* for keeping track of what's "open" */
    var activeClass = 'menu-open', showingDropdown, showingMenu, showingParent;
    /* hides the current menu */
    var hideMenu = function() {
        if(showingDropdown) {
            showingDropdown.removeClass(activeClass);
            showingMenu.fadeOut(500);
        }
    };

    /* recurse through dropdown menus */
    $('.drop').each(function() {
        /* track elements: menu, parent */
        var dropdown = $(this);
        var menu = dropdown.next('.subnav'), parent = dropdown.parent();
        /* function that shows THIS menu */
        var showMenu = function() {
            hideMenu();
            showingDropdown = dropdown.addClass('menu-open');
            showingMenu = menu.show();
            showingParent = parent;
        };
        /* function to show menu when clicked */
        dropdown.attr('href','/#dropdown').bind('click',function(e) {
            if(e) e.stopPropagation();
            if(e) e.preventDefault();
            if ( dropdown.hasClass('menu-open') ) {
                hideMenu();
            } else {
                showMenu();
            }
        });
        /* function to show menu when someone tabs to the box */
        dropdown.bind('focus',function() {
            showMenu();
        });

    });
});

It would be much appreciated if anyone could give me a pointer as to why IE would be behaving differently than Webkit & Firefox. 如果有人能给我指出为什么IE的行为方式不同于Webkit和Firefox,将不胜感激。 A link to an existing solution that does what I describe would also be great. 可以执行我所描述的操作的现有解决方案的链接也很好。 Please explain as if you were talking to a slow child, because jQuery / Javascript is not my forte. 请解释一下,好像您在跟一个慢孩子说话一样,因为jQuery / Javascript不是我的强项。

showMenu(); showMenu(); is getting called on focus (mouse down) then on "click" (mouse release) hideMenu(); 在焦点(鼠标向下)上被调用,然后在“单击”(鼠标释放)上被调用hideMenu(); is getting called which is why if you click on it again it works because it's already focused. 被调用,这就是为什么如果您再次单击它就会起作用,因为它已经聚焦。 but if you were to click off of it I bet you would get the the same behaviour.. I would remove 'focus' code imo or make "focus" so it doesn't interfere with "click" 但是,如果您单击它,我敢打赌,您也会得到相同的行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM