简体   繁体   English

jQuery show元素,其中包含具有类的元素

[英]JQuery show element which has an element with class in

I have a drop down menu ul when I am on the page I would like to have the drop down menu stay so I want to show the ul item only if it has a li element with the class ".current-menu-item" in it. 当我在页面上时,我有一个下拉菜单ul,我希望保留下拉菜单,因此,我只想在ul项具有类“ .current-menu-item”中的li元素时显示ul项它。 I found this code, but it would show all drop downs (.sub-menu) I only want to have the one with the ".current-menu-item" in it to be shown: 我找到了这段代码,但是它将显示所有下拉列表(.sub-menu),我只想显示其中带有“ .current-menu-item”的那些下拉列表:

if ($(".sub-menu").find(".current-menu-item").length > 0){ 
$(".sub-menu").css('display', 'block');
}

How can I achieve this? 我该如何实现?

只需使用:has选择器:

$(".sub-menu:has(.current-menu-item)").show();

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

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