1

JQueryを使用して「展開-展開解除」関数を作成しようとしました。ユーザーが展開をクリックすると、div の子が表示されます。ユーザーが unexpand をクリックすると、div の子が消えます。しかし、うまくいきません。

これが私のコードです:

$(document).ready(function(){
$('.expand').click(function(){
    var id = $(this).attr('id');

    $(this).html("-");
    $(this).attr("class", "unexpand");

    $.post("classes/ajax_laporan.php",
            {
                id: id
            },
            function(data){
            }
    );
});

$('.unexpand').click(function(){
   $(this).html("+");
   $(this).attr("class", "expand");
});
})

この問題を解決するのを手伝ってくれる人はいますか? 前もって感謝します。

4

1 に答える 1

4

class=unexpandDOM ロード時は with 要素が存在しないため、以下のように委譲する必要があると思います。

$(document).ready(function () {

    $(document).on('click', '.expand', function () {
        var id = $(this).attr('id');
        $(this).html("-");
        $(this).attr("class", "unexpand");
    });

    $(document).on('click', '.unexpand', function () {
        $(this).html("+");
        $(this).attr("class", "expand");
    });
});

フィドルのデモ

于 2013-09-21T14:36:32.633 に答える