jquery .toggle on the same div 但只打开一个

jquery .toggle on the same div but only open one

<div>
    <input class='kommentareBtn' type='button' value='Kommentare'>
    <div class='blogKommentare'>Halooooooooooo</div>
</div>

<div>
    <input class='kommentareBtn' type='button' value='Kommentare'>
    <div class='blogKommentare'>Halooooooooooo</div>
</div>


$(document).ready(function () {
    $(".kommentareBtn").click(function () {
        $(".blogKommentare").toggle();
    });
});

https://jsfiddle.net/xum1zfqo/

我希望在单击按钮 .kommentareBtn 时打开字段 .blogKommentare 但只有按钮下方的一个字段,而不是所有字段。

toggle() 方法已在 jQuery 1.8 版中弃用,并在 1.9 版中删除。如果您使用的是 1.8,下面的代码将起作用

   $(document).ready(function() {
        $(".kommentareBtn").click(function(){
            $(this).next(".blogKommentare").toggle();
        });
    });

请在此处查看 fiddle:

https://jsfiddle.net/xum1zfqo/9/

您可以使用它,但它仅适用于您的示例 html:

$(document).ready(function() {
    $(".kommentareBtn").click(function(){
        $(this).next(".blogKommentare").first().toggle();
    });
});

我不确定这是否有效,但我认为它会:

    $(document).ready(function() {
    $("#kommentareBtn").click(function(){
        $(this).next("#blogKommentare").toggle("slow");
    });
});

DEMO

More Example see the Link