jQuery parent().find() 问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5972618/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 20:07:51  来源:igfitidea点击:

jQuery parent().find() problem

jqueryjquery-selectorsslidetoggle

提问by Pav

HTML

HTML

<div class="comments">
    <a class="toggle" href="#">Toggle Comment 1</a><br />
    <div class="comment" style="display:none;">
        Comment1
    </div>
    <hr />
    <a class="toggle" href="#">Toggle Comment 2</a><br />
    <div class="comment" style="display:none;">
        Comment2
    </div>
</div>

JavaScript

JavaScript

$(function(){
    $('.toggle').click(function() {
        $(this).parent().find('.comment').slideToggle();
        return false;
    });
});

Can be viewed here: http://jsfiddle.net/saiprex/ESM4m/

可以在这里查看:http: //jsfiddle.net/saiprex/ESM4m/

How i can toggle commentthat's been clicked and not all of them?

我如何切换comment已点击的而不是全部?

Cheers, Pav

干杯,帕夫

回答by alex

$(function(){
    $('.toggle').click(function() {
        $(this).nextAll('.comment:first').slideToggle();
        return false;
    });
});

jsFiddle.

js小提琴

回答by radosch

its even simpler, I think, when you clean up your html as well a little bit: (avoid br)

我认为,当您稍微清理 html 时,它甚至更简单:(避免使用 br)

http://jsfiddle.net/ESM4m/27/

http://jsfiddle.net/ESM4m/27/

<div class="comments">
    <a class="toggle" href="Fork#">toggle</a>
    <div class="comment" style="display:none;">
        Comment1
    </div>
    <hr />
    <a class="toggle" href="#">toggle</a>
    <div class="comment" style="display:none;">
        Comment2
    </div>
</div>




$(function(){
    $('.toggle').click(function() {
        $(this).next().slideToggle();
        return false;
    }); 
});