Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/429.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Can';t从HTML表中获取列的名称_Javascript_Html Table - Fatal编程技术网

Javascript Can';t从HTML表中获取列的名称

Javascript Can';t从HTML表中获取列的名称,javascript,html-table,Javascript,Html Table,我曾经尝试过使用Javascript获取表的列的名称,但运气不佳。我只想在th标记中获取文本: <th data-visible="false" id="th-0">The column name</th> 我只得到一个错误: Uncaught TypeError: Cannot read property 'innerHTML' of null 当我尝试的时候什么都没有 var column = $('#th-'+key).text(); 关键变量来自ma循环,因

我曾经尝试过使用Javascript获取表的列的名称,但运气不佳。我只想在th标记中获取文本:

<th data-visible="false" id="th-0">The column name</th>
我只得到一个错误:

Uncaught TypeError: Cannot read property 'innerHTML' of null
当我尝试的时候什么都没有

var column = $('#th-'+key).text();
关键变量来自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('');
}
函数详细信息格式化程序(索引,行){
var html=[];
var columns_number=document.getElementById('table')。行[0]。cells.length/*http://stackoverflow.com/a/10043799/1883256*/
console.log('列数为:'+列数');
$.each(行、函数(键、值){
如果(!isNaN(键)){/*http://stackoverflow.com/a/9716580/1883256*/
log('键值为:'+键);
var column=$(“#th-”+键).text();
html.push(“”+key+”:“+value+”-列:“+column+”

”); } }); 返回html.join(“”); }

有人能解释一下如何解决这个问题吗?

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

您在jquery选择器
$('#th-'+键)中缺少“#”。text()
哦,是的,我尝试过使用和不使用,但都不起作用。在调用
getElementById
之前,尝试将
变量记录到控制台。我敢打赌这是一个变量值的问题。你能把HTML添加到你的问题中并做一个修改吗?
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('');
}