除了水平方向,儲存格內容的垂直對齊同樣會影響版面觀感。透過表格物件的 vAlign 屬性,我們可以讓列內容靠上、置中或靠下對齊。
下例示範如何把某一列的內容改為靠上對齊:
<!DOCTYPE html> <html> <head> <script> function topAlign() { document.getElementById('tr2').vAlign="top"; } </script> </head> <body> <table width="50%" border="1"> <tr id="tr1"> <th>Firstname</th> <th>Lastname</th> <th>Text</th> </tr> <tr id="tr2"> <td>Peter</td> <td>Griffin</td> <td>Hello my name is Peter Griffin. I need a long text for this example. I need a long text for this example.</td> </tr> </table> <br> <input type="button" onclick="topAlign()" value="Top-align table row" /> </body> </html>把 vAlign 改成 middle 或 bottom,即可切換為置中或靠底對齊。希望本文對你的 JavaScript 開發有所幫助。