简体   繁体   English

jQuery 隐藏仅适用于 Chrome Mac 和 IE,不适用于 Chrome PC 或 FF

[英]jQuery hide only working on Chrome Mac and IE, not Chrome PC or FF

I have a small function that I wrote to hide a submenu on a wordpress site that uses FoundationPress as a template and unfortunately I've discovered that it doesn't work on Chrome PC or FF.我编写了一个小函数,用于在使用 FoundationPress 作为模板的 wordpress 站点上隐藏子菜单,但不幸的是,我发现它在 Chrome PC 或 FF 上不起作用。 I initially used toggle to hide it but switched to .hide() and then even setting it to .css("display","none") but the problem still persists on the PC side.我最初使用切换来隐藏它但切换到 .hide() 然后甚至将其设置为 .css("display","none") 但问题仍然存在于 PC 端。

$(document).ready(function() {
    $('.top-bar-section li.active:not(.has-form) a:not(.button)').click(function() { 
        $("li.active ul.sub-menu.dropdown").hide();
    });
    $('.top-bar-section ul li').hover(function() {
        if($("li.active ul.sub-menu.dropdown").css("display", "none")){
           $("li.active ul.sub-menu.dropdown").css("display", "block");
        }
    });
})

What should happen is the user navigates to a section of the site and then uses the menu at the top to jump down to each section.应该发生的是,用户导航到站点的某个部分,然后使用顶部的菜单跳到每个部分。 So here: http://development.maclynutility.com/littlefriends/childrens-services/ if you use the menu to navigate anywhere within the current section (children's services) the menu stays active on a PC using Chrome or FF but hides on a Mac or IE.所以在这里: http : //development.maclynutility.com/littlefriends/childrens-services/如果您使用菜单导航当前部分(儿童服务)中的任何位置,菜单在使用 Chrome 或 FF 的 PC 上保持活动状态,但隐藏在Mac 或 IE。 I need it to hide all the time and work like it does on IE or Mac Chrome.我需要它一直隐藏并像在 IE 或 Mac Chrome 上一样工作。 Thanks for any help.谢谢你的帮助。

It's not working because it is not able to find the它不起作用,因为它无法找到

li.active ul.sub-menu.dropdown

Make sure what you are targetting.确定你的目标是什么。

if($("li.active ul.sub-menu.dropdown").css("display", "none")){
  $("li.active ul.sub-menu.dropdown").css("display", "block");
}

should be:应该:

if($("li.active ul.sub-menu.dropdown").css("display") === "none"){
  $("li.active ul.sub-menu.dropdown").css("display", "block");
}

since $("li.active ul.sub-menu.dropdown").css("display", "none") will always hide and return the selected dom elements, which are "truthy", and therefore the if body will be executed, showing the dropdown.由于$("li.active ul.sub-menu.dropdown").css("display", "none")将始终隐藏并返回选定的 dom 元素,这些元素是“真实的”,因此 if body 将是执行,显示下拉列表。

However, since it will either display if it's hidden, or do nothing if it's displayed, that function will essentially do nothing differently than if you completely omitted the if statement.但是,因为它要么显示它是隐藏的,要么显示它什么都不做,所以该函数本质上与完全省略 if 语句没有什么不同。 Did you intend for it to toggle?你打算让它切换吗? Or maybe to show while hovering but hide afterwards?或者也许在悬停时显示但之后隐藏?

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

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