[英]If div inside other divs hasClass, addClass to top level div
I have a question.我有个问题。 I currently have a bunch of divs inside my sidebar.我目前在我的侧边栏中有一堆 div。
The structure is like this: div.toggle-container > div > div.showme > div.berocket_single_filter_widget结构是这样的: div.toggle-container > div > div.showme > div.berocket_single_filter_widget
This divs are generated from a wordpress plugin to show product filters.此 div 是从 wordpress 插件生成的,用于显示产品过滤器。 Sometimes when a filter is empty the class below is added.有时,当过滤器为空时,会添加下面的类。
div.bapf_mt_none div.bapf_mt_none
So the structure then becomes:所以结构就变成了:
div.toggle-container > div > div.showme > div.berocket_single_filter_widget.div.bapf_mt_none div.toggle-container > div > div.showme > div.berocket_single_filter_widget.div.bapf_mt_none
Im trying to add a class with jQuery so that when if the class.div.bapf_mt_none exists inside these other divs.我试图用 jQuery 添加一个类,以便当这些其他 div 中存在 class.div.bapf_mt_none 时。 I want to add a class to div.toggle-container我想向 div.toggle-container 添加一个类
But im stuck.但我卡住了。
All help is appreciated.感谢所有帮助。
Thanks谢谢
I'm trying to add a class when the class.div.bapf_mt_none exists inside these other divs.当 class.div.bapf_mt_none 存在于这些其他 div 中时,我试图添加一个类。 I want to add a class to div.toggle-container我想向 div.toggle-container 添加一个类
You could go with a solution that takes the top .toggle-container
, then looks inside the structure to find where .berocket_single_filter_widget
has .bapf_mt_none
, alternatively, you could look directly for .bapf_mt_none
then navigate up to add the class to the container:您可以使用采用顶部.toggle-container
的解决方案,然后查看结构内部以查找.berocket_single_filter_widget
具有.bapf_mt_none
,或者,您可以直接查找.bapf_mt_none
然后向上导航以将类添加到容器中:
$(".bapf_mt_none").closest(".toggle-container").addClass(".empty");
(using .empty as an example) (以 .empty 为例)
If you find .bapf_mt_none
occurs in places other than your filter, you can be more specific in the selector, eg:如果您发现.bapf_mt_none
出现在过滤器以外的地方,您可以在选择器中更具体,例如:
$("div.toggle-container > div > div.showme > div.berocket_single_filter_widget.div.bapf_mt_none")
.closest(".toggle-container")
.addClass(".empty");
but the concept is the same - look for the bottom level and, thanks to the way jquery works, it will then apply to all matching parents for all elements found.但概念是相同的 - 寻找底层,由于 jquery 的工作方式,它将应用于所有找到的所有元素的匹配父级。 If none found, .empty
(in the example) won't be applied.如果没有找到, .empty
(在示例中)将不会被应用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.