2017-11-10 26 views
1

jQueryで子供の子供を選択できないのはなぜですか? .children()メソッドと>セレクタを使用して、子の子にクラスを追加したいと思います。以下のコードを参照してください:子供の子供を選択する

$(function(){ 
     // main expansion element 
     $(".expander").click(function() { 
      var subShown = $(this).children("ul > li").hasClass("show"); 
      if (!subShown) { 
       $(this).children(".indented").slideDown('100').addClass("show"); 
       $(this).children(".caret").addClass("reversedCaret"); 
      } else { 
       $(this).children(".indented").slideUp('100').removeClass("show"); 
       $(this).children(".caret").removeClass("reversedCaret"); 
      } 
     }); 
    }); 

HTML:私はexpanderをクリックすると

<div class="expander"> 
        <span class="caret downCaret right visibleCaret">+</span> 
        <ul> 
        <li class="category">Item 1<a href="http://www.google.com"></a></li> 
        <li class="indented"><a href="http://www.google.com">Item 2</a></li> 
        <li class="indented"><a href="http://www.google.com">Item 3</a></li> 
        </ul> 
       </div> 

それは私のli要素にクラスを追加しません。どうして? .children()JQuery documentationから

+2

'li'(何'子供であるエクスパンダ、のではない直接の子です) 'が返ってくる。 'children()'の代わりに 'find()'を使う必要があります。 'children()'は本質的に 'find( '> *')'と同じです。 – Taplar

+0

'.find()'を試しましたが、正しく動作させることができませんでした。それは '.children()'の前後に行きますか? – kawnah

+2

Scottのソリューションを見てください。あなたは子どもを()使ってはいけません。 – Taplar

答えて

2

.children()メソッドは、その.children中)(.findは異なる()のみ は(.findながらDOMツリーの単一レベルを下っ)は、子孫要素(孫、 など)を選択するために複数のレベルを下に を横断することができます。

実際には.children()を使用する必要はありません。はるかに簡単な解決策は、セレクタの後にJQueryに第2引数を渡すことで、ただprovide context for your queriesになります。

$(function(){ 
 
     // main expansion element 
 
     $(".expander").click(function() { 
 
     
 
      // Just for demonstration: ************************************************* 
 
      
 
      // 2 because of <span> and <ul> 
 
      console.log($(this).children().length);   
 
      
 
      // 0 because anything beyond children isn't returned in the first place 
 
      console.log($(this).children("ul > li").length); 
 
      // ************************************************************************* 
 
     
 
      // By passing "this" as the second argument to JQuery, after the selector, 
 
      // it sets the context for the search to only "this", so the entire DOM 
 
      // is not searched, just the object that "this" is bound to. 
 
      var subShown = $("ul > li", this).hasClass("show"); 
 
      if (!subShown) { 
 
       $(".indented", this).slideDown('100').addClass("show"); 
 
       $(".caret", this).addClass("reversedCaret"); 
 
      } else { 
 
       $(".indented", this).slideUp('100').removeClass("show"); 
 
       $(".caret", this).removeClass("reversedCaret"); 
 
      } 
 
     }); 
 
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="expander"> 
 
    <span class="caret downCaret right visibleCaret">+</span> 
 
    <ul> 
 
    <li class="category">Item 1<a href="http://www.google.com"></a></li> 
 
    <li class="indented"><a href="http://www.google.com">Item 2</a></li> 
 
    <li class="indented"><a href="http://www.google.com">Item 3</a></li> 
 
    </ul> 
 
</div>

+0

ありがとうございます。でも、私のバージョンがうまくいかなかった理由を理解するのに苦労していますか?したがって、 '.children()'がdomツリーの1つのレベルを下回ると、私のliはdivの2レベル下になります。だからそれを念頭に置いて、 '.children()'と '>'セレクタを使って私が望むものを与えてはならないはずです。 – kawnah

+0

@kawnah '.children()'メソッドは、**子要素のみ**のラップされたセットを返します。したがって、セレクタがそこから深く検索するように指定しても、検索が行われる要素のセットには、深度の低い要素はありません。 –

+1

@kawnah私は、 '.children()'を呼び出すと直下の子ノードのみを返し、それ以外は何も表示しないことを示すいくつかのデモンストレーションを含むように答えを更新しました。だから深く検索しようとすると何も返されません。 –

0

あなたはまだあなたが(与えられた答えは方法によって優れている、いないよう)しているとして、それに近づくにしたい場合は、変更するには、この

$(this).children("ul > li") 

これまで:

$(this).children('ul').children() 

の子供を選択するそれはです。

+0

ok ..私はScott Marcusの答えに前進していますが、ありがとう – kawnah

0

あなたは.find('>ul>li')、例を使用することができます。

var expander = $(".expander") 

expander.on('click', function(){ 
    var li = expender.find('>ul>li') 
    li.hasClass("show") 
}) 
0

.querySelector

document.querySelector("section > div").classList.add("my-gray");
.my-gray{ 
 
    background: gray; 
 
}
<section> 
 
Foo 
 
<div> 
 
Bar 
 
</div> 
 
</section>

関連する問題