2017-06-21 27 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()函數來動態添加行/列,但是您並沒有真正指定你在這方面想要做什麼。