カーソルを合わせたデータセルと行を色分けハイライト表示する表

マウスのポインタ(カーソル)を合わせたデータセルを赤色でハイライト表示し、そのセルが所属する行を黄色でハイライト表示する機能を、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 $sampleTBodies = $sampleTable.tBodies;

    for ( var $tbodyIndex = 0; $tbodyIndex < $sampleTBodies.length; $tbodyIndex++ ) {
        var $sampleTBodyRows = $sampleTBodies[$tbodyIndex].rows;
        for ( var $rowIndex = 0; $rowIndex < $sampleTBodyRows.length; $rowIndex++ ) {
            $sampleTBodyRows[$rowIndex].onmouseover = function(){
                this.style.backgroundColor = "yellow";
            };
            $sampleTBodyRows[$rowIndex].onmouseout = function(){
                this.style.backgroundColor = "";
            };
            var $sampleTBodyCells = $sampleTBodyRows[$rowIndex].cells;
            for ( var $cellIndex = 0; $cellIndex < $sampleTBodyCells.length; $cellIndex++ ) {
                $sampleTBodyCells[$cellIndex].onmouseover = function(){
                    this.style.backgroundColor = "red";
                };
                $sampleTBodyCells[$cellIndex].onmouseout = function(){
                    this.style.backgroundColor = "";
                };
            }
        }
    }

}
</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>

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>

スポンサード リンク

カテゴリー: JavaScript, , 逆引き パーマリンク