2016-03-08 39 views
1

我已成功在列上進行增量編號。創建具有列數增量編號的html表格

這裏我的代碼

table { 
 
    counter-reset: rowNumber; 
 
} 
 
table tr { 
 
    counter-increment: rowNumber; 
 
} 
 
table tr td:first-child::before { 
 
    content: counter(rowNumber); 
 
    min-width: 1em; 
 
    margin-right: 0.5em; 
 
}
<table border="1"> 
 
    <tr> 
 
     <th>No</th> <th>Name</th>  
 
    </tr> 
 
    <tr> 
 
     <td></td> <td>jhon</td> 
 
    </tr> 
 
    <tr> 
 
     <td></td> <td>lucy</td> 
 
    </tr> 
 
    <tr> 
 
     <td></td> <td>joe</td> 
 
    </tr> 
 
</table>

enter image description here

,但我想打表數字,如圖像下面

enter image description here

回答

0

table元素上,您的計數器爲1,在第一行上它會增加到2.因此從2開始。您需要在第一個tr上執行重置。

table tr { 
    counter-increment: rowNumber; 
} 
table tr:first { 
    counter-reset: rowNumber; 
} 
0

在第二個tr處重置計數器。

tr:nth-child(2) { 
 
    counter-reset: rowNumber; 
 
} 
 
table tr { 
 
    counter-increment: rowNumber; 
 
} 
 
table tr td:first-child::before { 
 
    content: counter(rowNumber); 
 
    min-width: 1em; 
 
    margin-right: 0.5em; 
 
}
<table border="1"> 
 
    <tr> 
 
    <th>No</th> 
 
    <th>Name</th> 
 
    </tr> 
 
    <tr> 
 
    <td></td> 
 
    <td>jhon</td> 
 
    </tr> 
 
    <tr> 
 
    <td></td> 
 
    <td>lucy</td> 
 
    </tr> 
 
    <tr> 
 
    <td></td> 
 
    <td>joe</td> 
 
    </tr> 
 
</table>

+0

感謝的很多尼克.. 但是當我imple認識到表與分頁號總是重置爲1 .. 你可以有解決方案 – Ahmad

0

這裏是一個解決您的問題的代碼。在你的代碼中,你正在計算行號爲2的行號。所以從2

其中一個解決您的問題的開始是使用theadtbody標籤,並增加從tbody行計數器作爲做了如下的解決方案。

table tbody { 
 
    counter-reset: rowNumber; 
 
} 
 
table tbody tr { 
 
    counter-increment: rowNumber; 
 
} 
 
table tr td:first-child::before { 
 
    content: counter(rowNumber); 
 
    min-width: 1em; 
 
    margin-right: 0.5em; 
 
}
<table border="1"> 
 
    <thead> 
 
    <tr> 
 
     <th>No</th> <th>Name</th>  
 
    </tr> 
 
    </thead> 
 
    <tbody> 
 
    <tr> 
 
     <td></td> <td>jhon</td> 
 
    </tr> 
 
    <tr> 
 
     <td></td> <td>lucy</td> 
 
    </tr> 
 
    <tr> 
 
     <td></td> <td>joe</td> 
 
    </tr> 
 
    </tbody> 
 
</table>

+0

非常感謝Narendra 但是當我執行到表與分頁號總是重置爲1 .. 你能解決嗎? – Ahmad

+0

你正在使用什麼分頁?請分享更多信息 –

1

您必須重設第二<tr>

櫃檯試試這個

table tr:nth-child(2){ 
    counter-reset: rowNumber; 
} 
table tr { 
    counter-increment: rowNumber; 
} 
table tr td:first-child::before { 
    content: counter(rowNumber); 
    min-width: 1em; 
    margin-right: 0.5em; 
} 

DEMO

+0

非常感謝Alexis 但是當我執行到分頁號表總是重置爲1 .. 你能解決嗎? – Ahmad

+0

您使css屬性的順序相同嗎?你的桌子上沒有任何課程?你的意思是分頁嗎?在頁面中,計數器重置爲1? – Alexis