2

我有一张非常不寻常的桌子。根据这个http://jsfiddle.net/yh6C6/,我正在努力能够返回特定元素的相对标头

表结构类似于以下:

<table border>
    <tbody>
        <tr>
            <th></th>
            <th></th>
            <th colspan="3">Parameter 1</th>
            <th colspan="2">Parameter 2</th>
        </tr>
        <tr>
            <td></td>
            <td></td>
            <td>
                <div>
                    <div>State 1.1</div>
                </div>
            </td>
            <td>
                <div>
                    <div>State 1.2</div>
                </div>
            </td>
            <td>
                <div>
                    <div>State 1.3</div>
                </div>
            </td>
            <td>
                <div>
                    <div>State 2.1</div>
                </div>
            </td>
            <td>
                <div>
                    <div>State 2.2</div>
                </div>
            </td>
        </tr>
        <tr>
            <th rowspan="2">Parameter 2</th>
            <td>State 2.1</td>
            <td contenteditable="true">-</td>
            <td contenteditable="true">-</td>
            <td contenteditable="true">-</td>
        </tr>
        <tr>
            <td>State 2.2</td>
            <td contenteditable="true">-</td>
            <td contenteditable="true">-</td>
            <td contenteditable="true">-</td>
        </tr>
        <tr>
            <th rowspan="3">Parameter 3</th>
            <td>State 3.1</td>
            <td contenteditable="true">-</td>
            <td contenteditable="true">-</td>
            <td contenteditable="true">-</td>
            <td contenteditable="true">-</td>
            <td contenteditable="true">-</td>
        </tr>
        <tr>
            <td>State 3.2</td>
            <td contenteditable="true">-</td>
            <td contenteditable="true">-</td>
            <td contenteditable="true">-</td>
            <td contenteditable="true">-</td>
            <td contenteditable="true">-</td>
        </tr>
        <tr>
            <td>State 3.3</td>
            <td contenteditable="true">-</td>
            <td contenteditable="true">-</td>
            <td contenteditable="true">-</td>
            <td contenteditable="true">-</td>
            <td contenteditable="true">-</td>
        </tr>
    </tbody>
</table>

假设您单击“contenteditable”单元格,我如何返回行和列标题?请检查上面提供的jsfiddle

4

2 回答 2

0

我已经编辑了小提琴以获取水平参数。需要为每个tr包含参数名称的类添加一个类。还对所选内容进行了着色,tr以便更轻松地可视化和调试所选内容。获取垂直参数名称将是一个不同的问题。

于 2013-11-08T16:39:21.587 回答
0

我能够通过执行以下操作来获得它:

$(document).ready(function () {
    $('table').on('click', 'td[contenteditable]', function (e) {

        // Horizontal Parameter
        if ($(this).parent().has('th').length > 0) {
            hParameter = $(this).parent().find('th').text();
        } else {
            hParameter = $(this).parent().prevAll('tr').has('th').first().find('th').text();
        }
        $('#horizontal-parameter').text('H: ' + hParameter);

        // Vertical Parameter
        var thLocator = [],
            colCount = 1;
        $table = $('table');
        $table.find('tr:first th').each(function () {
            for (var i = 0; i < this.colSpan; i++) {
                thLocator.push(colCount);
            }
            colCount++;
        });

        vParameter = $table.find('tr:first th:nth-child(' + thLocator[$(this).index()] + ')').text();

        $('#vertical-parameter').text('V: ' + vParameter);

    });

});
于 2013-11-08T17:08:51.737 回答