2013-05-09 2 views
12

정말 간단하다고 생각하지만 올바른 선택기 구문을 찾을 수없는 것 같습니다. 나는 이렇게 html로 가지고있다 :하위 항목 h2 텍스트에 특정 값이있는 요소 찾기

<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> 

h2 아이가 'Statistics'텍스트를 포함하고있는 div를 찾고 싶다. 찾기대로 작동하지 않습니다

$(".parent").find("h2[text='Statistics'").parent() 

가 빈 집합을 반환 :

나는의 다양한 조합을 시도했다. 내가 이렇게하면 :

$(".parent").find("h2").text("Statistics").parent() 

나는 3 개의 요소를 얻었는데, 모두가 통계를 포함하고있는 div처럼 보입니다. 나는 처음에 그것을 더할 수는 있지만, 그것은 총체적으로 보인다. 하나의 항목을 찾는 올바른 구문은 무엇입니까?

+0

'는 .text()가'당신이 그것을 사용하려고했습니다로서가 아닌 선택 또는 제한으로, 텍스트를 설정하는 데 사용됩니다. –

+0

@SetSailMedia Ah. 그럼 왜 그들이 모두 같았는지 설명 할 수 있습니다 :) – Nicros

+0

:}} @Joe에서 아래의 답변을 잘 선택하거나 비슷한 질문과 대답을 확인할 수 있습니다. http://stackoverflow.com/questions/813477/jquery-selector-where- 텍스트 - 일부 - 값 –

답변

18

사용 :

$(".parent").find("h2:contains('Statistics')").parent(); 

이것은 :contains() 선택기를 사용합니다.

+0

완벽, 고마워! – Nicros

+0

이것은 갈 길입니다! –

1
$(".parent").find("h2:contains('Statistics')").each(function(){ 
    var div=$(this).parent(); 
    // use div as you want 
}) 

근무 http://jsfiddle.net/SayjQ/

관련 문제