是否有一个iniherit“”选择器?

时间:2013-09-22 02:18:17

标签: html5 jquery-selectors html-table

在我制作的扫雷中,我创建了一个10 * 10 <table>并给了<td> s id s 0-99。但我想知道每个<td>是否有一个继承选择器,就像某种html5或jquery语法。

我的代码

document.getElementById("grid").innerHTML = "";

var gameBox = "";

    //ten rows
for ( i = 0 ; i < rowAmount ; i++ ) {
    gameBox += "<tr>";
        //ten columns
    for ( var j = 0 ; j < columnAmount ; j++ ) {
            //produce id value
        var idValue = ( i * rowAmount ) + j;
            //write cell
        gameBox += "<td class = 'box' id = '" + idValue + "' ></td>";
    }
    gameBox += "</tr>";
}
document.getElementById("grid").innerHTML = gameBox;

2 个答案:

答案 0 :(得分:4)

使用CSS选择行(行)中的td和可以使用的列(col)

$('tr:nth-child(['+ row +'])>td:nth-child(['+ col +'])');

因为你知道它是10乘10然后你可以使用简单的数学在表格下选择正确的td - 类似于

$('table.grid td:nth-child('+ row*10 + col +');

答案 1 :(得分:0)

您可以$('td')获取所有td元素,但理想情况下,您会在自己生成的每个td上添加一个课程,并且可以选择该类,以便您的代码在您的网页上的某处添加另一个table后不会中断