添加新元素属性

adding new element attribute

本文关键字:属性 元素 新元素 添加      更新时间:2024-03-19

我在表元素上设置了一个新属性(每个表都应该有selectedRow属性,它是指向所选tr元素的指针),但实际上在调用我的单击处理程序之间,该属性变成了null:

 $("table.grid").each(function () {
        this.selectedRow = null;
    });
    var selectRow = function (tr) {
        var table = tr.parents("table").get();
        if (table.selectedRow == tr.get()) return;
        // table.selectedRow still NULL!!!!!!!!!!!
        if (table.selectedRow) {
            var unselect = $(table.selectedRow);
            unselect.removeClass('selectedChilds');
            unselect.prev('tr').removeClass('siblingUpChilds');
            unselect.next('tr').removeClass('siblingDownChilds');
        }
        table.selectedRow = tr.get();
        tr.addClass('selectedChilds');
        tr.prev('tr').addClass('siblingUpChilds');
        tr.next('tr').addClass('siblingDownChilds');
    }
    $('table.grid tr').click(function (e) {
        selectRow($(e.delegateTarget));
    });

使用数据(key[,value])设置/获取属性:

$('#my-elem').data('selected_row', '...');