Javascript 如何为此表的单元格创建工作标识符?

Javascript 如何为此表的单元格创建工作标识符?,javascript,Javascript,我试图给表中的每个单元格一个标识符,如图所示 这是我尝试过的,但不起作用: function init() { var board = document.createElement('table'); board.setAttribute("border", 1); board.setAttribute("cellspacing", 0); board.setAttribute("id", 'tbl'); var identifier = 0;// &l

我试图给表中的每个单元格一个标识符,如图所示

这是我尝试过的,但不起作用:

function init() {
    var board = document.createElement('table');
    board.setAttribute("border", 1);
    board.setAttribute("cellspacing", 0);
    board.setAttribute("id", 'tbl');

    var identifier = 0;// <-------
    for (var i = 0; i < GRID_SIZE; i++) {
        var row = document.createElement('tr');
        board.appendChild(row);
        for (var j = 0; j < GRID_SIZE; j++) {
            var cell = document.createElement('td');
            cell.setAttribute('height', 20);
            cell.setAttribute('width', 20);
            cell.classList.add('col' + j, 'row' + i);
            cell.identifier = identifier; // <-------
            row.appendChild(cell);
            identifier += identifier;// <--------this is the problem
        }
    }

    document.getElementById("main").appendChild(board);
}
函数init(){
var board=document.createElement('table');
board.setAttribute(“边框”,1);
board.setAttribute(“单元间距”,0);
board.setAttribute(“id”,“tbl”);

var identifier=0;//首先,您不应该使用自定义属性,也不应该使用
width
height
属性。其次,您自己递增
identifier
,所以它不会从0开始增长

const GRID\u SIZE=6;
函数init(){
var board=document.createElement('table');
board.setAttribute(“边框”,1);
board.setAttribute(“单元间距”,0);
board.setAttribute(“id”,“tbl”);
var标识符=0;
对于(变量i=0;i
main>表td{
宽度:20px;
高度:20px;
}

首先,您不应该使用自定义属性,也不应该使用
宽度
高度
属性。其次,您自己在递增
标识符
,因此它不会从0开始增长

const GRID\u SIZE=6;
函数init(){
var board=document.createElement('table');
board.setAttribute(“边框”,1);
board.setAttribute(“单元间距”,0);
board.setAttribute(“id”,“tbl”);
var标识符=0;
对于(变量i=0;i
main>表td{
宽度:20px;
高度:20px;
}

网格大小*i+j;

使用
cell.innerHTML=GRID\u SIZE*i+j;

函数初始化(网格大小){
var board=document.createElement('table');
board.setAttribute(“边框”,1);
board.setAttribute(“单元间距”,0);
board.setAttribute(“id”,“tbl”);

var标识符=0;//
网格大小*i+j;

使用
cell.innerHTML=GRID\u SIZE*i+j;

函数初始化(网格大小){
var board=document.createElement('table');
board.setAttribute(“边框”,1);
board.setAttribute(“单元间距”,0);
board.setAttribute(“id”,“tbl”);

var identifier=0;//非常感谢,这很有效。有什么原因我不能使用自定义属性吗?顺便说一句,谢谢所有的建议。这被认为是一种不好的做法。将来,HTML标准可能会在元素对象中添加一个名为
identifier
的属性。aaaaah好的,以后不用做维护噩梦了,再次谢谢在中。非常感谢,这很有效。有什么原因我不能使用自定义属性吗?顺便说一句,谢谢所有的建议。这被认为是一种不好的做法。将来,HTML标准可能会在元素对象中添加一个名为
identifier
的属性。aaaah好的,以后不会有维护噩梦,再次感谢。