jquery - 当子 div 具有特定的类名时,隐藏 div 的类
问题描述
我通常尝试做的是,检测我们来自 Trustpilot Widget 的产品是否有 0 条评论,如果是,则显示来自 Trustpilot 的另一个小部件,它显示我们服务的评论数量。
现在,当在页面上找到一个名为“tp-stars--0”的 div 类时,我一直试图隐藏小部件。
这是我到目前为止得到的
$('.trustpilot-widget' ).ready(function() {
if($('.tp-widget-stars').find('.tp-stars.tp-stars--0').length) {
$('#tp-widget-wrapper').removeClass('visible');
}
});
<div id="tp-widget-wrapper" class="tp-widget-wrapper visible">
<div id="wrapper-company-stars" class="wrapper-company-stars">
<!-- Stars -->
<div id="tp-widget-stars" class="tp-widget-stars">
<div class="">
<div class="tp-stars tp-stars--0">
<div style="position: relative; height: 0; width: 100%;
padding: 0; padding-bottom: 18.326693227091635%;">
</div>
</div>
</div>
</div>
<div class="tp-widget-readmore" id="numberOfReviews"><strong>No
reviews</strong>
</div>
</div>
</div>
解决方案
尝试each
在元素上使用循环并使用closest
来获取父级。
$('.tp-widget-stars').find('.tp-stars.tp-stars--0').each(function() {
if ($(this).length) {
$(this).closest('#tp-widget-wrapper').removeClass('visible');
}
});
推荐阅读
- r - 素食主义者中的 R cca 和 predict.cca
- mysql - 遍历MYSQL中的数组
- matlab - MATLAB - 查找位于嵌套单元格数组中的条目的多维索引
- merge - 根据查找表中的值创建 SAS 变量
- arrays - 如何在 Swift 4 中通过另一个 Ints 数组对字符串数组进行排序
- vba - MS Access VBA:将字符串拆分为预定义的宽度
- excel - 将单元格中的值与数组进行比较,如果为真则添加值
- macos - karabiner 元素(和 xmodmap):使用左命令作为 ctrl 和右命令作为 alt 不适用于键盘上的 zxcv 行
- python - 为什么相同正则表达式的 python re.findall() 会为两个等效的输入字符串产生不同的结果?
- c - C中原子变量的比较