マウスのポインタ(カーソル)を合わせた列をハイライト表示する機能を、JavaScriptで、表(table要素)に実装する方法。
実装例
ヘッダ1 | ヘッダ2 | ヘッダ3 | ヘッダ4 | ヘッダ5 |
---|---|---|---|---|
1-1 | 1-2 | 1-3 | 1-4 | 1-5 |
2-1 | 2-2 | 2-3 | 2-4 | 2-5 |
3-1 | 3-2 | 3-3 | 3-4 | 3-5 |
フッタ1 | フッタ2 | フッタ3 | フッタ4 | フッタ5 |
ソースコード
JavaScript
<script type="text/javascript">
window.onload = function(){
var $sampleTable = document.getElementById( "sample" );
var $sampleColgroup = $sampleTable.getElementsByTagName( "colgroup" );
var $sampleTHeadCells = $sampleTable.tHead.rows[0].cells;
var $sampleTFootCells = $sampleTable.tFoot.rows[0].cells;
var $sampleRows = $sampleTable.rows;
for ( var $rowIndex = 0; $rowIndex < $sampleRows.length; $rowIndex++ ) {
var $sampleCells = $sampleRows[$rowIndex].cells;
for ( var $cellIndex = 0; $cellIndex < $sampleCells.length; $cellIndex++ ) {
$sampleCells[$cellIndex].onmouseover = function(){
changeBackgroundColor( $sampleColgroup[this.cellIndex], "yellow" );
changeBackgroundColor( $sampleTHeadCells[this.cellIndex], "yellow" );
changeBackgroundColor( $sampleTFootCells[this.cellIndex], "yellow" );
};
$sampleCells[$cellIndex].onmouseout = function(){
changeBackgroundColor( $sampleColgroup[this.cellIndex], "" );
changeBackgroundColor( $sampleTHeadCells[this.cellIndex], "" );
changeBackgroundColor( $sampleTFootCells[this.cellIndex], "" );
};
}
}
}
function changeBackgroundColor( $elementReference, $color ){
$elementReference.style.backgroundColor = $color;
}
</script>
window.onload = function(){
var $sampleTable = document.getElementById( "sample" );
var $sampleColgroup = $sampleTable.getElementsByTagName( "colgroup" );
var $sampleTHeadCells = $sampleTable.tHead.rows[0].cells;
var $sampleTFootCells = $sampleTable.tFoot.rows[0].cells;
var $sampleRows = $sampleTable.rows;
for ( var $rowIndex = 0; $rowIndex < $sampleRows.length; $rowIndex++ ) {
var $sampleCells = $sampleRows[$rowIndex].cells;
for ( var $cellIndex = 0; $cellIndex < $sampleCells.length; $cellIndex++ ) {
$sampleCells[$cellIndex].onmouseover = function(){
changeBackgroundColor( $sampleColgroup[this.cellIndex], "yellow" );
changeBackgroundColor( $sampleTHeadCells[this.cellIndex], "yellow" );
changeBackgroundColor( $sampleTFootCells[this.cellIndex], "yellow" );
};
$sampleCells[$cellIndex].onmouseout = function(){
changeBackgroundColor( $sampleColgroup[this.cellIndex], "" );
changeBackgroundColor( $sampleTHeadCells[this.cellIndex], "" );
changeBackgroundColor( $sampleTFootCells[this.cellIndex], "" );
};
}
}
}
function changeBackgroundColor( $elementReference, $color ){
$elementReference.style.backgroundColor = $color;
}
</script>
HTML
<table id="sample">
<caption id="sampleCaption">
表のタイトル
</caption>
<colgroup span="1"></colgroup>
<colgroup span="1"></colgroup>
<colgroup span="1"></colgroup>
<colgroup span="1"></colgroup>
<colgroup span="1"></colgroup>
<thead>
<tr>
<th>ヘッダ1</th>
<th>ヘッダ2</th>
<th>ヘッダ3</th>
<th>ヘッダ4</th>
<th>ヘッダ5</th>
</tr>
</thead>
<tbody>
<tr>
<td>1-1</td>
<td>1-2</td>
<td>1-3</td>
<td>1-4</td>
<td>1-5</td>
</tr>
<tr>
<td>2-1</td>
<td>2-2</td>
<td>2-3</td>
<td>2-4</td>
<td>2-5</td>
</tr>
<tr>
<td>3-1</td>
<td>3-2</td>
<td>3-3</td>
<td>3-4</td>
<td>3-5</td>
</tr>
</tbody>
<tfoot>
<tr>
<th>フッタ1</th>
<th>フッタ2</th>
<th>フッタ3</th>
<th>フッタ4</th>
<th>フッタ5</th>
</tr>
</tfoot>
</table>
<caption id="sampleCaption">
表のタイトル
</caption>
<colgroup span="1"></colgroup>
<colgroup span="1"></colgroup>
<colgroup span="1"></colgroup>
<colgroup span="1"></colgroup>
<colgroup span="1"></colgroup>
<thead>
<tr>
<th>ヘッダ1</th>
<th>ヘッダ2</th>
<th>ヘッダ3</th>
<th>ヘッダ4</th>
<th>ヘッダ5</th>
</tr>
</thead>
<tbody>
<tr>
<td>1-1</td>
<td>1-2</td>
<td>1-3</td>
<td>1-4</td>
<td>1-5</td>
</tr>
<tr>
<td>2-1</td>
<td>2-2</td>
<td>2-3</td>
<td>2-4</td>
<td>2-5</td>
</tr>
<tr>
<td>3-1</td>
<td>3-2</td>
<td>3-3</td>
<td>3-4</td>
<td>3-5</td>
</tr>
</tbody>
<tfoot>
<tr>
<th>フッタ1</th>
<th>フッタ2</th>
<th>フッタ3</th>
<th>フッタ4</th>
<th>フッタ5</th>
</tr>
</tfoot>
</table>
CSS
<style>
#sampleCaption {
border-top: double;
font-size: inherit;
margin: 0px;
padding: 3px;
background-color: lightgreen;
}
#sample tr,
#sample th,
#sample td {
background-color: inherit;
font-size: inherit !important;
color: inherit !important;
}
#sample thead,
#sample tfoot {
background-color: lightblue;
}
#sample {
border: double !important;
}
#sample thead {
border-bottom: double;
}
#sample tfoot {
border-top: double;
}
</style>
#sampleCaption {
border-top: double;
font-size: inherit;
margin: 0px;
padding: 3px;
background-color: lightgreen;
}
#sample tr,
#sample th,
#sample td {
background-color: inherit;
font-size: inherit !important;
color: inherit !important;
}
#sample thead,
#sample tfoot {
background-color: lightblue;
}
#sample {
border: double !important;
}
#sample thead {
border-bottom: double;
}
#sample tfoot {
border-top: double;
}
</style>