2017-08-16 7 views
0

親コメント内にネストされたアイコンを何度かクリックすると、親コメントを含むすべての子コメントを崩壊しようとしています。子要素に対してのみjQuery関数を実行するには?

以下のjQueryコードでは、コメントボックスの崩壊を得ることができましたが、別のセクション内にあるコメントも崩壊しています。

のjQueryコード -

$('.comment-toggle pre').on('click', function(e) { 
    $(".single-comment-wrapper .comment-text, .single-comment-wrapper .comment-bottom, .single-comment-outer .child-comment ").slideToggle('fast', function() { 
     if ($(this).is(':visible')) { 
      $(".comment-toggle pre").text('[–]'); 
     } else { 
      $(".comment-toggle pre").text('[+]'); 
     } 
    }); 
}); 
$('.comment-toggle pre').on('click', function(e) { 
    $('.single-comment-wrapper .left-side').slideToggle('fast'); 
}); 

HTMLand CSSが長すぎたため。私はcodepenを作成しました。以下はそれへの直接リンクです。事前に

https://codepen.io/anon/pen/Vzrvbm

感謝。

答えて

-1

私はdivと異なるクラスを使うべきだと思います。 .content-togleクラスをクリックすると、javascriptコードはすべての.single-comment-wrapper .comment-text、.single-comment-wrapper .comment-bottom、.single-comment-outer .child-commentクラスのアクションを実行します。

0

あなたのdivの構造は、これはトリッキーになり、私は〜10minsのためのフィドルに遊んでてきたし、これが出ている - 右方向の見出しが、完璧ではない...

$('.comment-toggle pre').on('click', function(e) { 
    $(this).parents('.single-comment-wrapper').next().slideToggle('fast', function() { 

現在、あなたのコードはクラスをターゲットにしているので、プラスとマイナスはすべて変更されます。+/+クリックされたように変更する必要があります。など

0

更新しますjQueryのあなたのクリックした要素に対する要素を検索する:

また
$('.comment-toggle pre').on('click', function(e) { 

    // find main comment element 
    var rootComment = $(this).closest('.single-comment-wrapper'); 

    // hide child comments of current comment 
    var children = rootComment.parent().find('>.child-comment'); 
    children.slideToggle('fast'); 

    // hide left part 
    rootComment.find('.left-side').slideToggle('fast'); 

    // hide current comment 
    rootComment.find('.comment-text').toggle('fast', function() { 
    if ($(this).is(':visible')) { 
     rootComment.find(".comment-toggle pre", this).text('[–]'); 
     } else { 
      rootComment.find(".comment-toggle pre", this).text('[+]'); 
     } 
    }); 
}); 

、あなたはそれがはるかに容易になるだろう、メインcomment要素のコンテキストに子要素が含まれるようにマークアップを変更することができる場合と連携。 ulに基づくツリーのようなビューは、マークアップを単純化し、HTML要素の量を減らす。

+0

あなたの答えをありがとうが、残念ながらコードは完全には機能しませんでした。まだ存在している問題 - a)コメントが折りたたまれているときは、http://i.imgur.com/BmL5tOX.pngのように表示されますが、代わりにこのように表示する必要があります - https:// i。 imgur.com/P7mhuYc.png b)2番目のコメント - (https://i.imgur.com/E6D7B4E.png)をクリックすると、その下のすべてのコメントの親コメントです。上記のコメントが崩壊していますそれだけでなく。 – rowdy

+0

答えが更新されました。それぞれのコメントは、それが隠されたコメントの内容を崩壊させたときに、その状態を保存します。最後に、 'parent-comment'クラスを削除することができます。 – Samich

関連する問題