gpt4 book ai didi

javascript - $.toggle() 类的所有实例

转载 作者:行者123 更新时间:2023-11-29 18:22:04 25 4
gpt4 key购买 nike

你好,我正在尝试切换隐藏显示评论,我最初让它工作得很好,因为我将我所有的评论都放在一个带有 .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- 发表了每条评论评论类。所以它只会打开和关闭第一条评论。其余的不受影响。我怎样才能改变这种行为,以便切换每个评论。?

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();

最佳答案

替换这一行

$(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") 将评估为空选择器。所以在这种情况下它不会起作用

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com