2016-08-20 321 views
-1

我有一個有1行5列的HTML表格。在第一列「行號」中,我想顯示哪一行。我在添加行和「添加行」按鈕。 實際的問題是:當我添加一個新行時,我想在第一列顯示哪一行(Ex.1)。請幫幫我。我可以使用哪些代碼。感謝大家幫助我。我將非常感謝我的問題的正確答案。行計數應該從一開始等。等等。每當我再次打開頁面時,它應該從一開始 - 而不是從最後一次開始。如何在特定列的HTML表格中顯示行號?

表名:myTable; 「添加行按鈕」name:addRowbtn; 再次感謝。

這是我曾嘗試 CSS代碼

body { counter-reset: Serial; /* Set the Serial counter to 0/} 
    table { border-collapse: separate; } 
    tr td:first-child:before { counter-increment: Serial;/Increment the Serial counter/content: "Serial is: " counter(Serial);/Display the counter */ } 

Here's a picture

+0

讓我們瞭解您已經嘗試 –

+0

體 { 計數器復位:串行;/*設置串行計數器爲0 */ } 表 border-collapse:separate; } tr td:first-child:before { counter-increment:Serial;/*遞增串行計數器*/ 內容:「串行是:」計數器(串行);/*顯示計數器*/ }/*這是我嘗試過的代碼*/CSS代碼 – user6738792

+0

使用此代碼編輯您的文章plz –

回答

0

下面是該工作代碼:

<html> 
    <head> 
     <script type="text/javascript"> 
     function displayResult() 
     { 
      var index = document.getElementById("myTable").rows.length; 
      var new_row = '<td>'+index+'</td><td>cell 1</td><td>cell 2</td>'; 
      document.getElementById("myTable").insertRow(-1).innerHTML = new_row; 
     } 
     </script> 
    </head> 

    <body>  
     <table id="myTable" border="1"> 
      <tr> 
      ` <td>0</td> 
       <td>cell 1</td> 
       <td>cell 2</td> 
      </tr> 

     </table> 
     <br /> 
     <button type="button" onclick="displayResult()">Insert new row</button>    
    </body> 
</html> 
0

沒有它怎麼你這樣做是很難說的代碼。 我假設行在一個集合中,因爲你有一個添加函數。 難道你不能只使用索引+ 1?

如果添加函數只是添加原始html,則可以獲取表格元素並計算子元素(或使用最後一個子元素)並從中計算您的數字。

隨着你給的信息很少,我可以說。

+0

我該怎麼做「您可以獲取表格元素並計算孩子(或使用最後一個孩子),並計算你的號碼「? – user6738792

+0

此外,這是一個代碼:[鏈接](https://jsfiddle.net/y9hn1stx/) – user6738792

+0

在你的小提琴中,你有表格標題在表格行內,這是不正確的,據我所知。 如果你改正了這個問題,你可以用下面的代碼得到表中的行數:var rows = document.getElementById(myTable).getElementsByTagName(「tr」).length;' – DevvoX

相關問題