2017-01-31 7 views
0

子供がfooを含む親要素を見つけるためにjqueryセレクタについて知りたい。Jquery要素の検索

私たちはxpathでこれを行うことができますが、それを行うjqueryの方法は何でしょうか?

おかげ

+0

http://api.jquery.com/contains-selector/ –

+0

'$( "*:( 'foo' で)含まれています")、親()' – Satpal

答えて

0

次の方法でフィールドの見つけセットをフィルタリングするためにjqueryのhasヘルパーを使用することができ、特定の子の基準

https://api.jquery.com/has/

console.log($('div').has('.foo').attr('id'))
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
<div id="c1"> 
 
    <span class="foo"></span> 
 
</div> 
 

 
<div id="c2"></div>

0

を持っている私は、子供たちを見つけるだろうし、そこから親に行く:

$(document).ready(function() { 
 
     var $parent = $(".foo").parent() 
 
     console.log($parent.attr("id")); 
 
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id="imTheParent"> 
 
    <span class="foo"></span> 
 
</div> 
 
<div id="imNotTheParent"> 
 
    <span class="bar"></span> 
 
</div>