在Javascript中应用Class OnChange

时间:2014-08-16 09:39:25

标签: javascript jquery html css

我在这样的javascript中创建了<li>... </li>没有

$(function () {
    var settings = {
        rows: 6,
        cols: 6,
        rowCssPrefix: 'row-',
        colCssPrefix: 'col-',
        seatWidth: 30,
        seatHeight: 30,
        seatCss: 'seat',
        selectedSeatCss: 'selectedSeat',
        selectingSeatCss: 'selectingSeat'
    };

    var init = function (reservedSeat) {
        var str = [], seatNo, className;
        for (i = 0; i < settings.rows; i++) {
            for (j = 0; j < settings.cols; j++) {

                seatNo = (i + j * settings.rows + 1);   
                className = settings.seatCss + ' ' + settings.rowCssPrefix + i.toString() + ' ' + settings.colCssPrefix + j.toString(); 
                str.push('<li id="'+seatNo+'" class="' + className  +" table"+seatNo+ '">' +'<a id="a'+ seatNo +'" title="'+ seatNo+'"></a>' '</li>');
            }
        }
        $('#place').html(str.join(''));

    };

现在我需要将班级selectedSeat应用于<li> ID = 15,14,25,10

2 个答案:

答案 0 :(得分:2)

迭代返回的数组,将类添加到所有指定的元素。

$.each(selectDB, function() {
    $("li#" + this).addClass(settings.selectedSeatCss);
});

DEMO

init函数中,您应该执行以下操作:

className = settings.seatCss + ' ' + settings.rowCssPrefix + i.toString() + ' ' + settings.colCssPrefix + j.toString(); 
if ($.inArray(seatNo, reservedSeat) != -1) {
    className += ' ' + settings.selectedSeatCss
};

答案 1 :(得分:1)

$.each(selectDB, function(i, v) {
    $('#' + v).addClass("selectedSeat");
});