查找子项h2文本具有特定值的元素

Nic*_*ros 14 jquery jquery-selectors

我认为这很简单,但我似乎无法找到正确的选择器语法.我有这样的HTML:

<div class="parent">
    <h2>Summary</h2>
    <p>... bunch of content ...</p>
</div>
<div class="parent">
    <h2>Statistics</h2>
    <p>... bunch of content ...</p>
</div>
<div class="parent">
    <h2>Outcome</h2>
    <p>... bunch of content ...</p>
</div>
Run Code Online (Sandbox Code Playgroud)

我想找到其h2子包含文本'Statistics'的div.

我尝试了各种组合:

$(".parent").find("h2[text='Statistics'").parent()
Run Code Online (Sandbox Code Playgroud)

这不起作用,因为find返回一个空集.如果我这样做:

$(".parent").find("h2").text("Statistics").parent()
Run Code Online (Sandbox Code Playgroud)

我找回了3个元素 - 所有这些元素似乎都是包含统计数据的div.我可以添加first(),但看起来有点粗糙.找到一个项目的正确语法是什么?

Joe*_*Joe 22

使用:

$(".parent").find("h2:contains('Statistics')").parent();
Run Code Online (Sandbox Code Playgroud)

这使用:contains()选择器.