2013-02-19 2 views
1

현재 javascript, html 및 css를 사용하여 웹 페이지를 디자인하고 있습니다. 이 페이지는 사용자가 좌석을 선택하거나 선택을 취소 할 수있는 좌석 예약 좌석입니다. 사용자가 시트를 선택했을 때 시트의 ID를 표시하도록 페이지를 가져 왔지만 사용자가 시트를 선택 취소하면 디스플레이에서 ID를 제거하는 데 문제가 있습니다.요소 추가 및 제거

다음은 DOM 요소가 아닌 속성에 자바 스크립트 코드

$('.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>"); 
     } 
    }); 
+0

는'$ 무엇입니까 ('#list')'? 선택한 좌석의 ID를 표시하는 'div'또는 'span'입니까? – Marcus

답변

1

remove() 제거합니다입니다. 그냥 빈 문자열로 속성을 설정 : 질문을 검토 한 후 $("#list").attr('id', '')

편집

를, 내가 오해 생각합니다. 실제 ID 속성이 아닌 ID를 표시하는 텍스트를 삭제 하시겠습니까?

이 경우 가장 쉬운 방법은 텍스트 노드를 일종의 요소로 래핑하여 제거를 쉽게 선택하는 것입니다.

당신이 문자열 ( $("#list").append($(this).attr('id') + "</br>");)를 추가

, 그래서 같은 기간에 출력을 포장 :

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

이 방법을 사용하면 같은를 제거 할 수 있습니다

$('#list').remove('#id');