如何获取单元格的相对标题?

时间:2013-11-08 16:35:17

标签: javascript jquery html css

我有一张非常不寻常的牌桌。我正在努力能够返回特定元素的相对标题,按照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

2 个答案:

答案 0 :(得分:0)

edited the fiddle获取水平参数。 需要为包含参数名称的每个tr添加一个类。还对选定的tr着色,以便更容易可视化和调试所选内容。获取垂直参数名称将是一个不同的问题。

答案 1 :(得分: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);

    });

});