简体   繁体   English

查找最接近的类实例

[英]Find closest instance of class

I have a list of comments with a reply option. 我有一个带有回复选项的评论列表。 When the reply button is clicked a form should appear below the comment. 单击回复按钮时,表单应显示在注释下方。

HTML: 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: CSS:

.test{display:none;}

jQuery: jQuery的:

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

I have tried using the following: 我尝试过使用以下内容:

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

Any help on this would be much appreciated , Thank you. 对此有任何帮助将非常感谢,谢谢。

If your html structure is same then you can try below, 如果您的html结构相同,那么您可以在下面尝试,

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

In case if there are some elements between the Reply section and the test then use .nextAll 如果在“ Reply部分和test之间存在某些元素,则使用.nextAll

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

Try this: 尝试这个:

If you have other closest elements and not only the comment with class .test , you need to make sure of getting the correct element doing the following: 如果你有其他最接近的元素,而不仅仅是类.test的注释,你需要确保获取正确的元素执行以下操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM