Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/374.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript $.toggle()类的所有实例_Javascript_Jquery - Fatal编程技术网

Javascript $.toggle()类的所有实例

Javascript $.toggle()类的所有实例,javascript,jquery,Javascript,Jquery,您好,我正在尝试切换隐藏显示评论,我最初让它工作得很好,因为我使用class。forum comments将我的所有评论都放在一个div中 但是,现在我将每个评论都分解为具有.forum评论的div类 这是原始代码: // Button dynamic comment show $("#forum").delegate(".btn-forum-comment-show", "click", function() { $(this).parent().next(".for

您好,我正在尝试切换隐藏显示评论,我最初让它工作得很好,因为我使用class
。forum comments
将我的所有评论都放在一个div中

但是,现在我将每个评论都分解为具有
.forum评论的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);
      }
    });
删去 作者:Jared De La Cruz于2013年7月9日星期二下午1:17:40 aae15c3ff2cf790b2c7fb61dbf1a83bfadbc59762622c4b39e25ba7e5fe434fd 第2条评论1 删去 默认情况下,使用
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>

欢迎来到论坛!讨论
作者:Jared De La Cruz于2013年7月7日星期日下午7:31:08
aae15c3ff2cf790b2c7fb61dbf1a83bfadbc59762622c4b39e25ba7e5fe434fd
这是学生论坛,任何学生都可以通过添加标签的选项发布信息!
删去
议论
显示
作者:Jared De La Cruz,2013年7月7日星期日下午7:31:20
aae15c3ff2cf790b2c7fb61dbf1a83bfadbc59762622c4b39e25ba7e5fe434fd
耶,评论!
删去
作者:Jared De La Cruz于2013年7月9日星期二下午1:17:40
aae15c3ff2cf790b2c7fb61dbf1a83bfadbc59762622c4b39e25ba7e5fe434fd
第2条评论1
删去

更新:创建一个新的类
。论坛评论
。论坛评论

var post=$(this).parents(“.forum comment”)

$(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。下一步(“.forum comments”)
将计算为空选择器。因此,在这种情况下它将不起作用

替换此行

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


最后一个评论部分将不能作为
$parent。下一步(“.forum comments”)
将计算为空选择器。因此在这种情况下它将不起作用

尝试使用
nextAll
而不是
next


检查尝试使用
nextAll
而不是
next


检查

您使用的jQuery版本是什么?从jQuery 1.7开始,
.delegate()
已被
.on()
方法所取代。乍一看,它就像是
.each()
的作业。查看你的.btn论坛评论show
。btn论坛评论show
比此代码高一步你使用的是哪个版本的jQuery?从jQuery 1.7开始,
.delegate()
已被
.on()
方法所取代。乍一看,它就像是
.each()
的作业。查看你的.btn论坛评论显示
。btn论坛评论显示
比此代码高一步这对我不起作用,它仍然只切换1,因为
。论坛评论
仅包含1条评论。我有多条评论这对我不起作用,它仍然只切换1,因为
。论坛评论
只包含1条评论。我有很多意见
var $parent = $(this).parent();
$parent.next(".forum-comments").length ? $parent.next(".forum-comments").toggle()
                                       : $parent.prev(".forum-comments").toggle()
$(this).parent().nextAll(".forum-comments").toggle();