选择要显示和隐藏的html类

时间:2013-06-30 22:28:46

标签: javascript jquery

我有以下HTML代码,我根据$.get JSON动态构建。

<div class="forum-post">
  <div>
    <span class="forum-title">Test post</span><span class="forum-type label label-forum-Discussion">Discussion</span>
  </div>

  <div class="forum-author">
    By: Jared De La Cruz on Thursday, June 27, 2013 7:17:43 PM
  </div>
  <pre>
    This is a test post
  </pre>

  <div class="btn-group">
    <a class="btn btn-mini btn-primary btn-forum-comment" id="51ccf2471238f1cc13000003"><i class="icon-comment icon-white"></i> comment</a>
  </div>

  <div class="btn-group">
    <a class="btn btn-mini btn-inverse btn-forum-comment-show" id="51ccf2471238f1cc13000003"><i class="icon-plus-sign icon-white"></i> show</a>
  </div>

  <div class="forum-comments">
    <div class="forum-author">
      By: Jared De La Cruz on Saturday, June 29, 2013 11:56:29 PM
    </div>
    <pre>
      This is a test comment
    </pre>
  </div>
  <hr style="border-top: 1px dotted #b0b0b0;border-bottom: 0px">
</div>

评论列表可能会变得很大。当用户点击show我希望论坛评论显示/隐藏。

// Button dynamic comment show
$("#forum").delegate(".btn-forum-comment-show", "click", function() {
  var post = $(this).parents(".forum-comments");
  console.log(post);
});

如何抓住所选帖子的选择器.forum-comments?与我$('#ID').hide();

类似

1 个答案:

答案 0 :(得分:2)

您可以使用parent / closest + next方法:

$("#forum").delegate(".btn-forum-comment-show", "click", function() {
   $(this).parent().next(".forum-comments").toggle();
});