jquery .toggle在同一个div上,但只打开一个

时间:2015-09-08 10:33:26

标签: jquery html toggle

<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/

如果我点击按钮.blogKommentare但是只按下按钮下的一个字段而不是所有字段,我想要打开字段.kommentareBtn

3 个答案:

答案 0 :(得分:2)

在jQuery 1.8版本中不推荐使用toggle()方法,在1.9版本中删除了该方法。如果你使用1.8以下代码将工作

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

请参阅此处的小提琴:

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

答案 1 :(得分:1)

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

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

答案 2 :(得分:0)

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

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

DEMO

More Example see the Link

相关问题