2013-02-19 119 views
1

我目前正在设计一个使用javascript,html和css的网页。该页面是座位预订座位,允许用户选择和取消选择座位。当用户选择座位时,我设法让页面显示座位的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>"); 
     } 
    }); 
+0

什么是'$( '#list')'?它是一个显示所选座位ID的'div'或'span'吗? – Marcus 2013-02-19 21:23:00

回答

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');