您好,我试图切换隐藏的显示评论,我本来可以正常运行,因为我在类.forum-comments的单个div中放了所有评论。

但是,现在我用具有.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();

最佳答案

尝试使用nextAll代替next

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


检查document

关于javascript - $ .toggle()一个类的所有实例,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17558984/

10-11 13:12