查找最接近的类实例

时间:2012-09-25 16:03:53

标签: jquery

我有一个带有回复选项的评论列表。单击回复按钮时,表单应显示在注释下方。

HTML:

<div id="" class="comment">
    <div class="comment-message">Message
        <div class="information"> 
            <a id="" class="comment-reply" href="/comments/reply/"> Reply </a>
        </div>
    </div>
</div>
<div class="test">Test</div>

CSS:

.test{display:none;}

jQuery的:

 $('.comment-reply').live("click", function(e) {
    e.preventDefault();
    $this = $(this);
    $this.closest('.test').toggle();
});

我尝试过使用以下内容:

$this.closest('.test').toggle();

对此有任何帮助将非常感谢,谢谢。

2 个答案:

答案 0 :(得分:3)

如果您的html结构相同,那么您可以在下面尝试,

$(this).closest('.comment').next().toggle()

如果Reply部分与test之间存在某些元素,请使用.nextAll

$(this).closest('.comment').nextAll('.test').toggle()

答案 1 :(得分:1)

试试这个:

如果您有其他最接近的元素而不仅仅是类.test的注释,则需要确保获取正确的元素执行以下操作:

$(this).closest('.comment').next('.test').toggle();