表格除了排版,本身就是個效果工具箱。以下幾組語法都不長,卻能解決不少實戰問題。
表格內部的線條可以選擇性隱藏,靠的是 rules 屬性:
<table border rules="cols" cellspacing="0" align="left"> /* 只顯示縱線,隱藏橫線 */ <table border rules="rows" cellspacing="0" align="right"> /* 只顯示橫線,隱藏縱線 */ <table border rules="none" cellspacing="0" align="center"> /* 全部隱藏 */
在 body 區放一個帶 id 的表格:
<table border="0" width="280" id="myexample" style="border:5px solid yellow"> <tr> <td>加入任意的物件。加入任意的物件。 <br>加入任意的物件。加入任意的物件。 <br>加入任意的物件。加入任意的物件.</td> </tr> </table>
接著用一個定時器在兩種顏色之間反覆切換:
<script language="JavaScript">
<!--
function flashit(){
if (!document.all) return
if (myexample.style.borderColor == "yellow")
myexample.style.borderColor = "lime"
else
myexample.style.borderColor = "yellow"
}
setInterval("flashit()", 500)
//-->
</script>
每 500 毫秒切換一次,看起來就是持續閃動。數值越小閃越快。
表格內容很大時,瀏覽器往往要等整張表都抓完才一次顯示,畫面會白很久。用 <tbody> 把表格切成幾段,就能一塊一塊地畫出來:
<table> <tbody> <tr><td>第一段內容</td></tr> <tr><td>第二段內容</td></tr> </tbody> <tbody> <tr><td>第三段內容</td></tr> <tr><td>第四段內容</td></tr> </tbody> </table>
這對資料量大的表格特別有感。
普通表格
<table border="1" width="220" style="position:absolute; left:11; top:11" height="26"> <tr><td width="100%">普通表格</td></tr> </table>
正立方表格——用 bordercolorlight 與 bordercolordark 做出明暗對比:
<table border="1" width="220" bordercolorlight="#eeeeee" bordercolordark="#000000"
style="position:absolute; left:10; top:49" height="26">
<tr><td width="100%">正立方表格</td></tr>
</table>
細表格——外框消失、只留一條細線:
<table border="0" frame="vsides" width="219" bgcolor="#000000"
cellspacing="1" cellpadding="0" height="22"
style="position:absolute; left:11; top:86">
<tr bgcolor="#FFFFFF"><td width="100%" height="2">細表格</td></tr>
</table>
立體表格——明暗反過來,做出凸起感:
<table border="1" width="220" bordercolorlight="#ffffff" bordercolordark="#ffffff"
style="position:absolute; left:10; top:112" height="34">
<tr><td width="100%" bgcolor="#B7B7B7"
bordercolorlight="#000000" bordercolordark="#eeeeee">立體表格</td></tr>
</table>
無名表格——借 fieldset 與 legend 做出帶標題的框:
<table width="220" align="center" style="position:absolute; left:246; top:12" height="51"> <tr><td> <fieldset style="width:220" align="center"> <legend>無名表格</legend> </fieldset> <br> </td></tr> </table>
這幾種風格的差異,全在 border、frame、rules 與 bordercolorlight/dark 的組合上。把它們調熟,之後要什麼框線都能快速湊出來。