2017-06-21 48 views
0

我需要JavaScript代码的帮助来动态添加行和列,并显示行和列号。动态。添加行和列以打开并显示行和列号

<!DOCTYPE html> 
    <html> 
    <head> 
    <style> 
    table, td { 
    border:1px solid black; 
    } 
</style> 
</head> 
<body> 

<p>Click on each tr element to alert its index position in the table:</p> 

    <table> 
    <tr onclick="myFunction(this)"> 
    <td>Click to show rowIndex</td> 
    </tr> 
    <tr onclick="myFunction(this)"> 
    <td>Click to show rowIndex</td> 
    </tr> 
    <tr onclick="myFunction(this)"> 
    <td>Click to show rowIndex</td> 
</tr> 
</table> 

<script> 
function myFunction(x) { 
alert("Row index is: " + x.rowIndex); 
} 
</script> 

</body> 

这是我已经试过,但我不知道是否该表格的行和列是动态生成的,也列不renerated和显示。 请帮忙。

+1

发布您尝试的代码。 –

回答

0

首先不要在事件处理程序手动添加到每个TD/tr元素,使用JQuery将其添加到所有TD元素:

$('td').click(function(e) { 
    console.log($(this).parent().index(), $(this).index()); 
}); 

在事件处理器,$(本).parent ().index()和$(this).index()将分别给出行号和列号。

我建议查看JQuery append()或clone()函数来动态添加行/列,但是您并没有真正指定你在这方面想要做什么。