javascript - 错误选择包含类的 div
问题描述
- 查找包含具有特定类的“a”元素的 div(此步骤已成功)
对于那个特定的 div(包含具有特定类的“a”元素),应该找到另一个类,将为其删除一个类。此步骤失败,因为所有包含类 filter-options-title 的 div 都将丢失“已关闭”类。
<script> require(["jquery"], function ($) { if ($("div.filter-optie").find('.shopby_link_selected')) { $("div.filter-optie").find('.filter-options-title').removeClass('closed'); //this should only happen when the 'a' element with class shopby_link_selected has been found, which is not happening right now with this piece of code. } }); </script>
有什么想法我可以实施以使其更强大,并使其实现我想要的吗?
附上HTML:
<div class="filter-optie">
<dt role="heading" aria-level="3" class="filter-options-title closed">
<label class="active" for="layered-filter-merk">BRAND</label>
</dt>
<dd class="filter-options-content">
<ol class="items shopby_filter_items_attr_merk">
<form data-shopby-filter="attr_merk" data-shopby-filter-request-var="merk">
<li class="item" data-label="LABEL">
<a class="shopby_filter_item_5cd9ce0ea7fbd shopby_link_selected" href="">
<input name="shopby[brand][]" value="3989" type="radio" style="" checked="">
<span class="label">XXXX</span>
<span class="count">4<span class="filter-count-label">PRODUCTS</span></span>
</a>
</li>
</form>
</ol>
</dd>
</div>
解决方案
@DigitalJedi,几乎是正确的,因为这.closed
将从所有有 a 的地方删除该类,.shopby_link_selected
但我相信根据您的 html,DOM 遍历是不正确的。
$("div.filter-optie .shopby_link_selected").each(function() {
$(this).closest('.filter-optie).find('.filter-options-title').removeClass('closed');
});
这将找到所有带有 class 的锚标记.shopby_link_selected
,然后对于它们中的每一个,它将在 DOM 中搜索父级.filter-optie
,然后在 DOM 中搜索.filter-options-title
并删除.closed
该类。
推荐阅读
- react-native - 如果文本输入为空字符串,则在设备上反应本机禁用提交键盘
- javascript - React Native - 如何舍入动画组件的值?
- xml - 使用包含多种类型的 XSLT 解析字符串
- ruby - Ruby Net::SSH 重用会话
- c++ - 为什么 GCC 8.2 编译在软浮点源期间抛出错误?
- python - SQLAlchemy Harvesine 大圆计算错误
- javascript - 使用 Vue.js:当没有“下一张”照片可用时,禁用图片幻灯片上的按钮
- javascript - 未捕获的类型错误:无法读取 null 的属性“隐藏”
- c++ - 2048 - 如何阻止我的图块合并两次?
- makefile - 即使已经有另一个与目标匹配的规则,模式隐式规则也会执行吗?