1

現在、javascript、html、css を使用して Web ページをデザインしています。このページは、ユーザーが座席を選択および選択解除できる座席予約シートです。ユーザーが座席を選択したときにページに座席の ID を表示することはできましたが、ユーザーが座席の選択を解除した場合に ID を表示から削除しようとすると問題が発生します。

以下はJavaScriptコードです

    $('.available, .unavailable, .selected').click(function(){
        var ID = $(this).attr('class');
        if (ID == 'n unavailable' || ID == 'p unavailable') {
            alert ('Seat is already booked. Please select another seat.');
        }
        else if (ID == 'n selected' || ID == 'p selected') {
            alert ('You have now unselected this seat.');
            $(this).html('<img src = "free.gif"/>');
            $(this).removeClass('selected').addClass('available');
            y--;
            $("#seats").html("Number of seats selected: " + y);
            $("#list").remove($(this).attr('id'));
        }
        else {
            alert ('You have now reserved this seat. You can unselect it by clicking the seat again.');
            $(this).html('<img src = "selected.gif"/>');
            $(this).removeClass('available').addClass('selected');
            y++;
            $("#seats").html("Number of seats selected: " + y);
            $("#list").append($(this).attr('id') + "</br>");
        }
    });
4

1 に答える 1

1

remove()属性ではなく、dom 要素を削除します。属性を空の文字列に設定するだけです:$("#list").attr('id', '')

編集

あなたの質問を見直した後、私は誤解していると思います。実際の id 属性ではなく、ID を表示するテキストを削除しますか?

この場合、最も簡単な方法は、削除するために選択しやすくするために、テキスト ノードを何らかの要素でラップすることです。

文字列 ( ) を追加する場所$("#list").append($(this).attr('id') + "</br>");では、次のように出力をスパンでラップします。

$("#list").append('<div class="id">' + $(this).attr('id') + "</div>");

このようにして、次のように削除できます。

$('#list').remove('#id');
于 2013-02-19T17:24:34.540 に答える