jQuery - 检查子 div 是否可见

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10406094/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 09:16:24  来源:igfitidea点击:

jQuery - Check if child div is visible

jquery

提问by kel

I'm trying to see if the the child div of an li is visible and if so apply a class to the li. This is what I got but it's not working.

我正在尝试查看 li 的子 div 是否可见,如果是,则将类应用于 li。这是我得到的,但它不起作用。

if(jQuery('#menu li').children('div').css('display') != 'none') {
    jQuery('li', this).addClass('dropHover');
}

回答by gdoron is supporting Monica

if (jQuery('#menu li > div').is(':visible')){
    //...
    jQuery('li', this).addClass('dropHover');
}

I don't know what is the DOM structure or to what thisrefers to, but this might do the trick as well:

我不知道什么是 DOM 结构或this指的是什么,但这也可以解决问题:

jQuery('#menu li:has(div:visible)').addClass('dropHover');

? ? It adds the class "dropHover" to all <li>elements that have a visible <div>
and they need to be children of an element with the menu id.

? ? 它将类“dropHover”添加到所有<li>具有可见元素的元素,<div>
并且它们需要是具有菜单 ID 的元素的子元素。