2013-03-13 35 views
0

我有一個表格,每行包含三列,我想爲每一行添加一個鏈接在旁邊。html把鏈接放在每個表格行旁邊

<table> 
    <th> 
     Name: 
    </th> 
    <th> 
     Price: 
    </th> 
    <th> 
     Description 
    </th> 

    <?php while ($row = $foods->fetch()) { ?> 
     <tr> 
      <td> 
       <?php echo $row['Name']; ?> 
      </td> 
      <td> 
       <?php echo $row['foodPrice']; ?> 
      </td> 
      <td> 
       <?php echo $row['foodDescription']; ?> 
      </td> 
     <a class="editLink">roma</a> 
     </tr> 
    <?php } ?> 
</table> 

我想最後td後添加一個鏈接a但鏈接成爲在表的頂部, 在此先感謝

+1

爲什麼你只是不添加其他​​每個保持聯繫? – Razmig 2013-03-13 08:53:03

+0

,因爲這使得所有的tds的寬度相同 – 2013-03-13 09:00:13

+0

你可以添加類到特定的​​來控制寬度嗎? – Razmig 2013-03-13 09:02:07

回答

1

您需要添加一個表格單元格包含的鏈接。此外,th標籤應該被tr標籤包圍。

<table> 
    <tr> 
     <th> 
      Name: 
     </th> 
     <th> 
      Price: 
     </th> 
     <th> 
      Description 
     </th> 
     <th> 
     </th> 
    </tr> 

    <?php while ($row = $foods->fetch()) { ?> 
     <tr> 
      <td> 
       <?php echo $row['Name']; ?> 
      </td> 
      <td> 
       <?php echo $row['foodPrice']; ?> 
      </td> 
      <td> 
       <?php echo $row['foodDescription']; ?> 
      </td> 
      <td class="link"><!-- Your missing this --> 
       <a class="editLink">roma</a> 
      </td><!-- and this --> 
     </tr> 
    <?php } ?> 
</table> 

要設置不同於其他鏈接的單元格,您需要使用CSS。將link的CSS類添加到td,其中包含您的a標記。然後如下定義樣式:

CSS

table{ 
    text-align: center; 
} 

td{ 
    width: 200px; 
} 

td.link{ 
    width: 50px; 
} 

工作實例:http://jsfiddle.net/y9C3G/

+0

以下添加一個代碼片段,這是我已經嘗試過的鏈接,因爲在表格頂部,而不是在行旁邊 – 2013-03-13 09:00:50

+0

@MarcoDinatsoli你在帖子中的例子顯示你沒有用' td'標籤,我的示例略有不同,鏈接被'td'標籤包圍。在我的標記 – 2013-03-13 09:02:12

+0

中尋找評論是的,它知道這種方式,但在你的方式所有的領域有相同的寬度,我希望鏈接寬度小,因爲它只是一個鏈接,它沒有任何意義如果它的寬度等於一個字段,如說明字段 – 2013-03-13 09:05:31

1

它是無效的有<a>標籤直接內<tr>這樣。通過將鏈接放置在其自己的單元格中,將另一列添加到表格中。您可能還想在第一行添加另一個<th>,或者將colspan="2"屬性添加到說明1。

另外,表需要包含一個<tbody>元素,我建議把你的<th> S IN一個<thead>

<table> 
    <thead> 
    ... 
    </thead> 
    <tbody> 
    ... 
    </tbody> 
</table> 
+0

什麼是colsan?如果我添加一個新的TD,這使得行中的所有單元格具有相同的寬度,我不希望這樣,因爲它只是一個鏈接,我有一個字段包含大量的單詞 – 2013-03-13 09:02:55

+0

一個colspan指示一個單元格它應該跨越多少列。例如,如果您在該單元格上設置了'rowspan =「5」',那麼可以有一個包含五列的表格,但一行包含一個單元格。 – 2013-03-13 13:10:42

1
<tr> 
    <td>Your Content</td> 
    <td>Your Content</td> 
    <td>Your Content</td> 
    <td class="myLinkClass"><a href=""></a></td> 
</tr> 

<style> 
    .myLinkClass { 
     width: 100px; /* This will give width for only the td with this class */ 
    } 
</style>