透過 `
<!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 值 | CSS vertical-align |
|---|---|
| top | top |
| middle | middle |
| bottom | bottom |
| baseline | baseline |
/* CSS */
tr.valign-top td { vertical-align: top; }
tr.valign-middle td { vertical-align: middle; }
tr.valign-bottom td { vertical-align: bottom; }
// JS 切換整列 class
document.getElementById('tr2').className = 'valign-top';
<select onchange="setRowAlign(this.value)">
<option value="top">頂部</option>
<option value="middle" selected>置中</option>
<option value="bottom">底部</option>
</select>
<script>
function setRowAlign(val) {
var row = document.getElementById('tr2');
row.className = 'valign-' + val;
}
</script>