使用Javascript将列和行的名称显示为坐标

使用Javascript将列和行的名称显示为坐标,javascript,Javascript,嗨,伙计们,我只是想问一下,我如何显示我的表的行和列名,这有点像excel?下面是我在互联网上找到的示例代码,但它不起作用。谢谢:) 用于添加新行的代码 <input type="button" id="add" value="Add row" onclick="Javascript:appendRow()"> <input type="button" id="add" value="Add column" onclick="Javascript:appendC

嗨,伙计们,我只是想问一下,我如何显示我的表的行和列名,这有点像excel?下面是我在互联网上找到的示例代码,但它不起作用。谢谢:)

用于添加新行的代码

    <input type="button" id="add" value="Add row" onclick="Javascript:appendRow()">
    <input type="button" id="add" value="Add column" onclick="Javascript:appendColumn()">



<script type="text/javascript">
// append row to the HTML table
     function appendRow() {
         var tbl = document.getElementById('my-table'), // table reference
             row = tbl.insertRow(tbl.rows.length),      // append table row
             i;
        // insert table cells to the new row
        for (i = 0; i < tbl.rows[0].cells.length; i++) {
            createCell(row.insertCell(i), i, 'row');
        }
     }
</script>

//将行追加到HTML表
函数appendRow(){
var tbl=document.getElementById('my-table'),//表引用
row=tbl.insertRow(tbl.rows.length),//追加表行
我
//将表格单元格插入新行
对于(i=0;i

单击此处查看

我试图实现代码,但我不确定IE的setAttribute中的行数和列数的功能

$("#test-table tr").each(function() {
    $('td', this).each(function () {
         createCell(this, $(this).parent().index() + ',' + $(this).index(), "red");
     })
})
function createCell(cell, text, style) {
    var column_num = parseInt( $(this).index() );
    var row_num = parseInt( $(this).parent().index() );
    var div = document.createElement('div'), // create DIV element
        txt = document.createTextNode(text); // create text node
    div.appendChild(txt);                    // append text node to the DIV
    div.setAttribute('class', style);        // set DIV class attribute
    div.setAttribute('row_num','column_num',style);    // set DIV class attribute for IE (?!)
    cell.appendChild(div);                   // append DIV to the table cell
    cell.setAttribute('row_num', 'column_num');
}

检查实施情况

@JacobGeorge我该怎么做?有任何示例项目吗?您是否尝试动态填充表并显示它?我不理解这个计划的核心目的code@JacobGeorge是:)这就是我的目的:)你对jQuery满意吗?或者你使用普通的JavaScript?@MikayilAbdullayev如果可能的话,只使用普通的JavaScript我看到了修改,但它可以作为表格的坐标吗?而不是测试词?请看我在你答案中的图片。谢谢:)这确实是正确的,但是缺少了一些东西,因为用坐标标记的那些是已经存在的,因为我还试图通过“添加”按钮添加行和列。不幸的是,每当我添加列和行时,它们都没有被标记。当你添加一个新的单元格时,必须再次调用该函数。你是如何添加单元格的?我正在通过按钮添加单元格。
$("#test-table tr").each(function() {
    $('td', this).each(function () {
         createCell(this, $(this).parent().index() + ',' + $(this).index(), "red");
     })
})
function createCell(cell, text, style) {
    var column_num = parseInt( $(this).index() );
    var row_num = parseInt( $(this).parent().index() );
    var div = document.createElement('div'), // create DIV element
        txt = document.createTextNode(text); // create text node
    div.appendChild(txt);                    // append text node to the DIV
    div.setAttribute('class', style);        // set DIV class attribute
    div.setAttribute('row_num','column_num',style);    // set DIV class attribute for IE (?!)
    cell.appendChild(div);                   // append DIV to the table cell
    cell.setAttribute('row_num', 'column_num');
}