jquery
  • jquery-selectors
  • find
  • 2011-08-14 6 views 4 likes 
    4

    グーグルでtry-and-erroringして、拡張可能なツリーのいくつかのラベル要素の1つにアクセスするためにjqueryセレクタを試してみましたか?子要素を見つけるためのjqueryセレクタ

    <span id="ppown" class="treeTitleSpan" role="leaf">PPO</span> 
    <span class="treeCheckBoxLabel"> 
        <label class='nodeCheckIcon' /> 
    

    またはこのような:

    <span id="ppw" class="treeTitleSpan" role="leaf">Other</span> 
    <span class="handle closed"></span> 
    <span class="treeCheckBoxLabel"> 
        <label class='nodeCheckIcon' /> 
    

    そして、私は(クリックハンドラで)それぞれの場合に.treeTitleSpanスパンから始めていたHTMLは次のようにどちらかです。

    $(this).next().next().children('.nodeCheckIcon'); 
    

    そして、他:私は私が1にアクセスすることができます知っている

    $(this).next().children('.nodeCheckIcon'); 
    

    しかし、私は両方のケースのために働くだろう単一のセレクタをしたいと思い、「検索」動作するようには思えません:

    $(this).find('.nodeCheckIcon:first') 
    

    助けてください。

    リック

    答えて

    0
    $(this).nextAll('.treeCheckBoxLabel').children('.nodeCheckIcon'); 
    

    それとも、複数のラベルを持っている可能性がある場合のように:

    <span id="ppw" class="treeTitleSpan" role="leaf">Other</span> 
    <span class="handle closed"></span> 
    <span class="treeCheckBoxLabel"> 
        <label class='nodeCheckIcon' /> 
    </span> 
    <span class="treeCheckBoxLabel"> 
        <label class='nodeCheckIcon' /> 
    </span> 
    

    そして、あなたは最初の一つだけ欲しい:

    $(this).nextAll('.treeCheckBoxLabel').first().children('.nodeCheckIcon'); 
    
    +0

    作品もありがとうございます。 – at7000ft

    0

    どのようにこのような何かについて:

    $(this).nextAll('.treeCheckBoxLabel:first').children('.nodeCheckIcon'); 
    

    :firstを使用すると、多くの場合、.treeCheckBoxLabelの1つのみが選択されます。残りのHTMLによっては問題になる場合もあります。 thislabelの親の兄弟であるspan要素を指しているので、$(this).find('.nodeCheckIcon:first')は仕事に行くされていない他の記事(猫:)肌には多くの方法)に

    +0

    それは素晴らしい作品です、Kenに感謝します。 – at7000ft

    0

    代替

    $(this).siblings(".treeCheckBoxLabel:has(label.nodeCheckIcon):first") 
    
    0

    。完全なマークアップ構造はわかりませんが、このスパン要素のラッピングコンテナがあれば、これを試すことができます

    $(this).parent().find('.nodeCheckIcon:first'); 
    
    関連する問題