How can I find each table cell's “visual location” using jQuery?

后端 未结 5 795
小蘑菇
小蘑菇 2020-12-09 05:56

I have a HTML table that contains both ROWSPANs and COLSPANs.

How can I find each cell\'s \"visual location\" using jQuery?

For example, here\'s a visual rep

5条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-09 06:31

    for this specific layout (i.e. if your first row doesn't have colspan and more or less uniform cell borders) you may do it this way:

    function getCellLocation(cell)
    {
        var row_number = cell.parentNode.rowIndex;
        var col_number = "";
        $(cell).parents('table').find('tr:first th').each( function()
        {
            if (cell.offsetLeft >= this.offsetLeft)
            {
                col_number = $(this).text();
            }
            else return false;
        });
        return col_number + row_number;
    }
    

    if you want numeric column change it to

    var col_number = 0;
        ...
    if (cell.offsetLeft >= this.offsetLeft)
    {
        col_number++;
    }
    

提交回复
热议问题