如何将绝对列表项置于无序列表中?

时间:2014-09-30 15:03:08

标签: javascript jquery html css

我正在尝试使用链接和无序列表项构建座位预订系统。平面图中的行彼此不同。第1排有22个座位,第2排有24个座位等。我试图将列表项目居中,但我无法弄明白。

这是我的代码:

$(function generateSeats() {

  var settings = {
    rowCssPrefix: 'row-',
    colCssPrefix: 'col-',
    seatWidth: 22,
    seatHeight: 22,
    seatCss: 'seat',
    selectedSeatCss: 'selectedSeat',
    selectingSeatCss: 'selectingSeat'
  };

  var init = function(reservedSeat) {
    var col = [22, 24, 24, 24, 24, 26, 28, 30, 30, 30, 30, 30, 28, 26, 26, 24, 24, 20, 16, 14];
    var str = [],
      seatNo, className;

    for (i = 0; i < col.length; i++) {
      for (j = 0; j < col[i]; j++) {
        seatNo = (i + j * settings.rows + 1);
        className = settings.seatCss + ' ' + settings.rowCssPrefix + i.toString() + ' ' + settings.colCssPrefix + j.toString();
        if ($.isArray(reservedSeat) && $.inArray(seatNo, reservedSeat) != -1) {
          className += ' ' + settings.selectedSeatCss;
        }
        str.push('<li class="' + className + '"' +
          'style="top:' + (i * settings.seatHeight).toString() + 'px;left:' + (j * settings.seatWidth).toString() + 'px">' +
          '<a title="' + seatNo + '">' + seatNo + '</a>' +
          '</li>');
      }
    }
    $('#place').html(str.join(''));
  };

  var bookedSeats = [];
  init(bookedSeats);

  $('.' + settings.seatCss).click(function() {
    if ($(this).hasClass(settings.selectedSeatCss)) {
      alert('Deze stoel is al bezet');
    } else {
      $(this).toggleClass(settings.selectingSeatCss);
    }
  });

  $('#btnShow').click(function() {
    var str = [];
    $.each($('#place li.' + settings.selectedSeatCss + ' a, #place li.' + settings.selectingSeatCss + ' a'), function(index, value) {
      str.push($(this).attr('title'));
    });
    alert(str.join(','));
  })

  $('#btnShowNew').click(function() {
    var str = [],
      item;
    $.each($('#place li.' + settings.selectingSeatCss + ' a'), function(index, value) {
      item = $(this).attr('title');
      str.push(item);
    });
    alert(str.join(','));
  })
});
    #holder {
      height: 447px;
      width: 667px;
      background-color: #F5F5F5;
      margin: auto;
      border: 1px solid #A4A4A4;
      position: relative;
    }
    #place {
      display: table-row;
    }
    #place li {
      display: table-cell;
      vertical-align: middle;
      list-style-type: none;
      position: absolute;
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="holder">
  <ul id="place">

  </ul>
</div>

<div class="button">
  <input type="button" id="btnShowNew" value="Keuze" />
</div>

http://jsfiddle.net/0bb4j5q7/

2 个答案:

答案 0 :(得分:1)

我建议您为每一行使用ul并将其置于中心位置。现在的方式是,标记在单个ul内没有线条概念,因此您无法将它们居中。

答案 1 :(得分:0)

感谢您的帮助。我用父母的“relative”和列表项的“absolute-align:middle”修复了它。

    #holder{    
    height: 447px;   
    width: 667px;
    background-color: #F5F5F5;
    margin: auto;
    border: 1px solid #A4A4A4;        
    position: relative;
}

#place {
    display:  table-row;
}

#place li {    
    display:  table-cell;
    vertical-align: middle;
    list-style-type:  none;
    position: absolute;  
}