DIV CSS 佈局教程網

JS控制表格只顯示橫線或只顯示縱線的方法
編輯:關於JavaScript     

本文示範如何控制表格只顯示列分隔線,或只顯示行分隔線。

關鍵是 table 元素的 rules 屬性:設為 "rows" 只顯示橫向分隔線,設為 "cols" 只顯示縱向分隔線。

 <!DOCTYPE html> <html> <head> <script> function rowRules() { document.getElementById('myTable').rules="rows"; } function colRules() { document.getElementById('myTable').rules="cols"; } </script> </head> <body> <table id="myTable" border="1"> <tr> <td>Row1 cell1</td> <td>Row1 cell2</td> </tr> <tr> <td>Row2 cell1</td> <td>Row2 cell2</td> </tr> <tr> <td>Row3 cell1</td> <td>Row3 cell2</td> </tr> </table> <br> <input type="button" onclick="rowRules()" value="Show only row borders"> <input type="button" onclick="colRules()" value="Show only col borders"> </body> </html>

rules 除了 rows 與 cols,還支援 all(全部)、none(全部隱藏)與 groups(僅在 tbody/thead 等分組之間顯示)。

實務提醒:這個屬性在 HTML5 中已不建議使用,且必須搭配 border-collapse: collapse 才會生效。若要在新專案達到同樣效果,用 CSS 的 border-collapse: collapse 加上針對 td 的 border-bottom 或 border-right 會更直觀。

希望本文所述對大家的 JavaScript 程式設計有所幫助。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved