简体   繁体   English

无法从 HTML 表格中获取列的名称

[英]Can't get the name of a column from an HTML Table

I have tried so bad with no luck to get the name of a column of a table by using Javascript.我已经尝试过很糟糕,但没有运气通过使用 Javascript 获取表的列的名称。 I just want to get the text inside a th tag:我只想获取 th 标签内的文本:

<th data-visible="false" id="th-0">The column name</th>

I have tried this:我试过这个:

var columna = document.getElementById('th-'+key).innerHTML;

And I just get either the error:我只是得到错误:

Uncaught TypeError: Cannot read property 'innerHTML' of null

or nothing when I try或者当我尝试时什么都不做

var column = $('#th-'+key).text();

The key var comes fro ma loop because I am using bootstrap table where i want to show additional information if the user clicks on a plus button:关键 var 来自 ma 循环,因为我正在使用引导表,如果用户单击加号按钮,我想在其中显示其他信息:

function detailFormatter(index, row) {
    var html = [];
    var columns_number=document.getElementById('table').rows[0].cells.length;/*http://stackoverflow.com/a/10043799/1883256*/
    console.log('The number of columns is: '+columns_number);
    $.each(row, function (key, value) {
        if(!isNaN(key)){/*http://stackoverflow.com/a/9716580/1883256*/
         console.log('Key value is: '+key);                    
         var column = $('#th-'+key).text();
                html.push('<p><b>' + key + ':</b> ' + value + ' - column: '+ column +'</p>');
        }
    });
    return html.join('');
}

Can anyone shed some light to fix this?任何人都可以解决这个问题吗?

尝试在选择器的开头插入# ,如下所示: var columna = $('#th-'+key).text();

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

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