简体   繁体   English

获取单击列中第一个单元格的内容以及HTML表格中单击行的第一个单元格

[英]Get contents of the first cell in the clicked column and the first cell of the clicked row in a HTML table

I'm using the following javascript to get the data in a clicked table cell: 我正在使用以下javascript来获取单击的表格单元格中的数据:

var table = document.getElementById('ThisWeek'),
    cells = table.getElementsByTagName('td');

for (var i=0,len=cells.length; i<len; i++)
{
    cells[i].onclick = function()
    {
        document.getElementById("txtVal").value = this.innerText;
    }
}

How can I modify this so I can also obtain the contents of the first cell in the clicked column and the first cell in the clicked row? 我该如何修改这个以便我也可以获得点击列中第一个单元格的内容和单击行中的第一个单元格? Eg if I click on "s" in the table below, I get the results "2" and "B" returned as two variables: 例如,如果我点击下表中的“s”,我得到结果“2”和“B”作为两个变量返回:

0 1 2 3
A q w e
B a s d
C z x c

Please note that I need a javascript solution, not jQuery. 请注意,我需要一个javascript解决方案,而不是jQuery。 Ideally, I would also like clicks on the first row and first column to return empty strings. 理想情况下,我还想点击第一行和第一列返回空字符串。

This snippet uses the properties I've mentioned in my comment: 此代码段使用我在评论中提到的属性:

window.onload = function () {
    var table = document.getElementById('table');
    table.addEventListener('click', function (e) {
        var target = e.target,
            col = target.cellIndex,
            row;
        while (target = target.parentElement) {
            if (!col && col !== 0) {
                col = target.cellIndex;
            }
            if (target.tagName.toLowerCase() === 'tr') {
                row = target.rowIndex;
                break;
            }               
        }
        console.log(table.rows[row].cells[0].innerHTML + ', ' + table.rows[0].cells[col].innerHTML);
    });
}

A live demo at jsFiddle . jsFiddle的现场演示

I'd suggest: 我建议:

function index(c){
    var i = 0;
    while (c.previousSibling){
        /* if the previousSibling has a nodeType and that nodeType === 1 
           (indicating the previousSibling is an element) increment the i variable */
        if (c.previousSibling.nodeType && c.previousSibling.nodeType === 1){
            i++;
        }
        // reset c to the previousSibling
        c = c.previousSibling;
    }
    /* i is the count of previous-siblings, and therefore the index
       amongst those siblings: */
    return i;
}

function getHeaders(e){
    /* this is the table element,
       e.target is the clicked element */
    var el = e.target,
        text = 'textContent' in document ? 'textContent' : 'innerText',
        headers = [el.parentNode.getElementsByTagName('td')[0][text],this.getElementsByTagName('tr')[0].getElementsByTagName('td')[index(el)][text]];
    // set the text of the nextElementSibling of the table:
    this.nextElementSibling[text] =  headers.join(', ');
}

document.getElementById('table').addEventListener('click', getHeaders);

JS Fiddle demo . JS小提琴演示

You can add this to your cells[i].onclick function: 你可以将它添加到你的单元格[i] .onclick函数:

var row = this.parentElement; // TR
var table = row.parentElement.parentElement; // TBODY > TABLE
document.getElementById("columnVal").value = row.rowIndex && this.cellIndex ? table.rows[0].cells[this.cellIndex].innerText : "";
document.getElementById("rowVal").value = row.rowIndex && this.cellIndex ? row.cells[0].innerText : "";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM