jquery - 使用 JQuery 的 find 方法,我如何根据“foo”的子元素的内容选择某个 HTML 元素“foo”的兄弟?
问题描述
假设我有如下 HTML:
<div class="foo-container">
<div class="foo">
<div class="foo-child">
<span>The Important Label</span>
</div>
</div>
<div class="elem-to-manipulate">
<p>I want to manipulate the style in this because it's the sibling of "The Important Label"</p>
</div>
</div>
p
基于它的兄弟有 text 的事实,我如何定位该元素The Important Label
。
我遇到了类似的事情
$('.root').find('.foo > .foo-child > span:contains("The Important Label")')
这成功地让我得到了span
元素。但是现在我如何获得p
兄弟中的元素div
?有没有办法告诉找到哪个父母得到兄弟姐妹?
解决方案
$('.root').find('.foo:has(> .foo-child > span:contains("The Important Label")) + div > p')
aaa:has(bbb)
检查是否有任何匹配aaa bbb
,然后才匹配aaa
+
匹配先前匹配元素的以下兄弟
(根据您的用例,您可能对.foo:contains("The Important Label") + div > p
...感到满意)
推荐阅读
- azure-rm - 如何使用二头肌将父资源名称引用到模块内的资源
- tinymce - TinyMCE:Span Tag 不能包含 a、i、br 元素。不是继续跨度,而是拆分
- tensorflow - 在 Arduino nano 上编译模型时出现 Tensorflowlite 错误
- python - Python 9x9 和 3x3 数组验证不包括 0
- laravel - Gitpod托管的laravel应用程序自动显示laravel安装页面而不显示主页
- xml - 如何根据使用情况定义具有空 $value 的元素?
- json - 如何在数组元素具有多种类型的 Go 中解析 JSON?
- visual-studio-code - 有什么方法可以让某些片段在没有确认的情况下自动扩展?
- python - 使用 Python OpenCV 从视差/深度图计算尺度模糊因子
- javascript - Javascript选中复选框结果?