$ .toggle()类的所有实例

时间:2013-07-09 21:57:12

标签: javascript jquery

您好我正在尝试切换隐藏显示评论,我原本让它工作得很好,因为我所有的评论都在一个div中,类.forum-comments

但是,现在我正在使用.forum-comments

的div来分解每条评论

这是原始代码:

   // Button dynamic comment show
    $("#forum").delegate(".btn-forum-comment-show", "click", function() {
      $(this).parent().next(".forum-comments").toggle();
      var change = $(this).parent().find(".btn-forum-comment-show").text();
      var show = '<i class="icon-plus-sign icon-white"></i> show';
      var hide = '<i class="icon-minus-sign icon-white"></i> hide';
      if(change == ' show')
      {
        $(this).parent().find(".btn-forum-comment-show").html(hide);
      }
      if(change == ' hide')
      {
        $(this).parent().find(".btn-forum-comment-show").html(show);
      }
    });

函数$(this).parent().next(".forum-comments").toggle();停止工作,因为我对.forum-comments类发表了每条评论。所以它只会打开和关闭第一个评论。其余的不受影响。如何更改此行为以便切换每个注释。?

html

    <div class="forum-comments" >
      <div class="forum-author">By: Jared De La Cruz on Sunday, July 07, 2013 7:31:20 PM</div>
      <div class="forum-owner">aae15c3ff2cf790b2c7fb61dbf1a83bfadbc59762622c4b39e25ba7e5fe434fd</div>
      <pre class="forum-body">yay comments!</pre>
      <div class="btn-group">
      <a class="btn btn-mini btn-danger btn-forum-comment-del-comment" id=""><i class="icon-remove-sign icon-white"></i> delete</a>
      </div>
    </div>
    <div class="forum-comments" >
      <div class="forum-author">By: Jared De La Cruz on Tuesday, July 09, 2013 1:17:40 PM</div>
      <div class="forum-owner">aae15c3ff2cf790b2c7fb61dbf1a83bfadbc59762622c4b39e25ba7e5fe434fd</div>
      <pre class="forum-body">2nd comment1</pre>
      <div class="btn-group"><a class="btn btn-mini btn-danger btn-forum-comment-del-comment" id=""><i class="icon-remove-sign icon-white"></i> delete</a></div>
    </div>

默认情况下,这些评论会隐藏style="display:none"并启用/禁用$.toggle()

这是整个部分的转储:

<div class="forum-post">
        <div><span class="forum-title">Welcome to the forum!</span><span class="forum-type label label-forum-Discussion">Discussion</span></div>
        <div class="forum-author">By: Jared De La Cruz on Sunday, July 07, 2013 7:31:08 PM</div>
        <div class="forum-owner">aae15c3ff2cf790b2c7fb61dbf1a83bfadbc59762622c4b39e25ba7e5fe434fd</div>
        <pre>This is the student forum, where any student can post information with the option to add tags!</pre>
        <div class="btn-group"><a id="" class="btn btn-mini btn-danger btn-forum-comment-del"><i class="icon-remove-sign icon-white"></i> delete</a></div>
        <div class="btn-group"><a id="" class="btn btn-mini btn-primary btn-forum-comment"><i class="icon-comment icon-white"></i> comment</a></div>
        <div class="btn-group"><a id="" class="btn btn-mini btn-inverse btn-forum-comment-show"><i class="icon-plus-sign icon-white"></i> show</a></div>
        <div class="forum-comments" style="display: block;">
            <div class="forum-author">By: Jared De La Cruz on Sunday, July 07, 2013 7:31:20 PM</div>
            <div class="forum-owner">aae15c3ff2cf790b2c7fb61dbf1a83bfadbc59762622c4b39e25ba7e5fe434fd</div>
            <pre class="forum-body">yay comments!</pre>
            <div class="btn-group"><a id="" class="btn btn-mini btn-danger btn-forum-comment-del-comment"><i class="icon-remove-sign icon-white"></i> delete</a></div>
        </div>
        <div class="forum-comments">
            <div class="forum-author">By: Jared De La Cruz on Tuesday, July 09, 2013 1:17:40 PM</div>
            <div class="forum-owner">aae15c3ff2cf790b2c7fb61dbf1a83bfadbc59762622c4b39e25ba7e5fe434fd</div>
            <pre class="forum-body">2nd comment1</pre>
            <div class="btn-group"><a id="" class="btn btn-mini btn-danger btn-forum-comment-del-comment"><i class="icon-remove-sign icon-white"></i> delete</a></div>
        </div>
        <hr style="border-top: 1px dotted #b0b0b0;border-bottom: 0px">
    </div>

更新:通过

创建新课程.forum-comments.forum-comment

var post = $(this).parents(".forum-comment");

$(this).parent().next(".forum-comments").toggle();

2 个答案:

答案 0 :(得分:3)

替换此行

$(this).parent().next(".forum-comments").toggle();

var $parent = $(this).parent();
$parent.next(".forum-comments").length ? $parent.next(".forum-comments").toggle()
                                       : $parent.prev(".forum-comments").toggle()

最后一条评论部分无效,因为$parent.next(".forum-comments")将评估为空选择器。所以在这种情况下它不起作用

答案 1 :(得分:1)

尝试使用nextAll代替next

$(this).parent().nextAll(".forum-comments").toggle();

检查document