2014-05-18 41 views
0

當使用CSS將多列表格變爲一列時,如何使數據的標籤 由附加信息自動創建?CSS將表格自動標記爲一列標記

This article即將在屏幕變爲 狹窄時將多列表製成一列。但「標籤數據」部分是硬編碼的。

/* 
Label the data 
*/ 
td:nth-of-type(1):before { content: "First Name"; } 
td:nth-of-type(2):before { content: "Last Name"; } 
td:nth-of-type(3):before { content: "Job Title"; } 
td:nth-of-type(4):before { content: "Favorite Color"; } 
td:nth-of-type(5):before { content: "Wars of Trek?"; } 
td:nth-of-type(6):before { content: "Porn Name"; } 
td:nth-of-type(7):before { content: "Date of Birth"; } 
td:nth-of-type(8):before { content: "Dream Vacation City"; } 
td:nth-of-type(9):before { content: "GPA"; } 
td:nth-of-type(10):before { content: "Arbitrary Data"; } 

難道我們自動從THEAD標籤,在這裏使用它們,而不是硬編碼?

<thead> 
<tr> 
    <th>First Name</th> 
    <th>Last Name</th> 
    <th>Job Title</th> 
    <th>Favorite Color</th> 
    <th>Wars or Trek?</th> 
    <th>Porn Name</th> 
    <th>Date of Birth</th> 
    <th>Dream Vacation City</th> 
    <th>GPA</th> 
    <th>Arbitrary Data</th> 
</tr> 
    </thead> 

附件是responsive_table.html程序。

+0

這是可能的,看這裏瞭解如何使用JS操縱僞元素:http://stackoverflow.com/問題/ 5041494 /操縱-CSS-僞元素-使用-jquery的-EG-之前和之後 – sjkm

回答

0

使用HTML5屬性:

#some-id td:before { content: attr(data-title); } 


#some-id td:before { 
/* Now like a table header */ 
position: absolute; 
/* Top/left values mimic padding */ 
top: 1px; 
left: 6px; 
width: 45%; 
padding-right: 10px; 
white-space: nowrap; 
text-align:left; 
font-weight: bold; 
} 

然後:

<tr class="padding"> 
    <th> 
      sth 
    </th> 
<tr> 
<tr> 
     <td data-title="sth"> 

     </td> 
</tr>