為Table中的thead加上邊框


出發點其實很簡單,就是想做個這樣的表:

 
所以就先架構table的html代碼:
  1.         <table cellspacing="0">
  2.             <thead>
  3.                 <tr><th>姓名</th><th>性別</th><th>居住地</th></tr>
  4.             </thead>
  5.             <tbody>
  6.                 <tr><td>張三</td><td>男</td><td>北京胡同</td></tr>
  7.                 <tr><td>李四</td><td>女</td><td>上海電視塔</td></tr>
  8.                 <tr><td>王五</td><td>男</td><td>錦州小菜廠</td></tr>
  9.                 <tr><td>趙六</td><td>女</td><td>沈陽五街</td></tr>
  10.                 <tr><td>陳七</td><td>男</td><td>武漢地鐵</td></tr>
  11.             </tbody>
  12.         </table>
隨后,設置CSS樣式:
  1. table{
  2.     width:400px;
  3.     text-align:center;
  4. }
  5. table thead{
  6.     border-bottom:2px solid #000080;
  7. }
  8. .even{
  9.     background:#c0c0c0;
  10. }
  11. .odd{
  12.     background:#a6caf0;
  13. }
  14. .firsttr{
  15.     background:#ffff00;
  16. }
再綁定jQuery效果:
  1.     $("tbody tr:even").addClass("even");
  2.     $("tbody tr:odd").addClass("odd");
  3.     $("tr:eq(0)").addClass("firsttr");
可是,沒有看到thead的下邊框啊,如圖:
 
然后,當然我很迷惑,發現thead這個東西啥都沒效果,當時我心里就有個七八分明白了,最后為了確定,google到了一個外國人的帖子,《Why border of <th> and <thead> both not showing here?》,這個叫Jitendra Vyas的人也和我有同樣的困惑。
接着,有人回復了,“You can't style the <thead> itself. It's not a visible element, so any style that you give it will only be visible when it's inherited by it's children.”
說的很對,意思是thead這個標記,是非可見的,你對它的設置最多只能反映到它包含的后代元素中去。
 
現在,明白了,繼續試吧,改成thead tr:
  1. table thead tr{
  2.     border-bottom:2px solid #000080;
  3. }
正當我要得意的時候,慘了,還是沒效果:
 
我去,繼續改:
  1. table thead th{
  2.     border-bottom:2px solid #000080;
  3. }
哎,沒問題了,用th就ok,如圖:
你們不覺得奇怪么?
現在我又陷入了困境,tr肯定是可見元素啊。。。
換用新的CSS:
  1. table{
  2.     width:400px;
  3.     text-align:center;
  4. }
  5. .even{
  6.     background:#c0c0c0;
  7. }
  8. .odd{
  9.     background:#a6caf0;
  10. }
  11. .firsttr{
  12.     background:#ffff00;
  13.     border:2px solid #000080;
  14. }
結果背景出來了,但border沒有。
別泄氣,繼續查看,相關的文章都指向同一個線索:border-collapse,我把這個CSS加入:
  1. table{
  2.     width:400px;
  3.     text-align:center;
  4.     border-collapse:collapse;
  5. }
結果出來了:
 
再回過頭來看看,border-collapse屬性設置表格的邊框是否被合並為一個單一的邊框,還是象在標准的 HTML 中那樣分開顯示。
在本例中,collapse就可以,而seperate就不行。其實seperate本來是想獨立展示出邊框的,collapse想合並隱去。怎么到這里就反過來了呢?
真的是用thead的人少,最后看到一篇這個:
《border-collapse實現表格細線邊框》,是個好辦法,但仍然沒有解決這個thead中表現相反的問題。
 
誰知道,就給我留言吧。
 
試了試 最后用的

table{
border:1px solid black;
width: 200px;
height: 200px;
text-align: center;
border-collapse:collapse;
}
table thead tr{
border-bottom:2px solid black;

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM