如何使用子文本引用父级?

时间:2011-06-02 20:22:09

标签: javascript jquery jquery-selectors

在jQuery中,如何仅引用子项h2等于“Recipe Test 2”的元素?

<li class="sfnewsListItem">
            <h2 class="sfnewsTitle">
                <a href="#">Recipe Test 1</a>
            </h2>
            <div class="sfnewsMetaInfo">May 23, 2011</div>            
</li>
<li class="sfnewsListItem">
            <h2 class="sfnewsTitle">
                <a href="#">Recipe Test 2</a>
            </h2>
            <div class="sfnewsMetaInfo">May 23, 2011</div>            
</li>
<li class="sfnewsListItem">
            <h2 class="sfnewsTitle">
                <a href="#">Recipe Test 3</a>
            </h2>
            <div class="sfnewsMetaInfo">May 23, 2011</div>            
</li>

2 个答案:

答案 0 :(得分:2)

你可以尝试:

$('a:contains("Recipe Test 2")').closest('h2');

JS Fiddle demo

参考文献:

答案 1 :(得分:2)

试试这个:

var parnt =$("h2:contains('Recipe Test 2')").closest("li");