鼠标移动到table,使其在鼠标下面的行高亮显示。

这段代码展示了如何创建一个JavaScript类`TableRowHilighter`,该类用于监听表格的鼠标事件,当鼠标移动到表格行上时,该行会高亮显示。作者Neil Chen在2005年9月5日编写了这个功能,允许指定初始高亮行和自定义高亮颜色。此外,还提供了辅助函数`highlightRow`和`unhighlightRow`用于手动控制行的高亮状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

///////////////////////////////////////////////////////////////////////////////
//
// 功能:这个类使得被附加的表格可以支持行点击高亮
// 参数:
//            tbl:                要附加样式的 table.
//            selectedRowIndex:    初始高亮的行的索引(从 0 开始). 此参数可省。
//            hilightColor:        高亮颜色。可省(默认为绿色)
//
// Author:    Neil Chen
// Date:    2005-09-05
//
///////////////////////////////////////////////////////////////////////////////
function TableRowHilighter(tbl, selectedRowIndex, hilightColor) {
    this.currentRow = null;
    this.hilightColor = hilightColor ? hilightColor : 'green';   

    if (selectedRowIndex != null
        && selectedRowIndex >= 0
        && selectedRowIndex < tbl.rows.length)
    {
        this.currentRow = tbl.rows[selectedRowIndex];       
        //tbl.rows[selectedRowIndex].runtimeStyle.backgroundColor = this.hilightColor;
    }

    var _this = this;
    var _thisTable = tbl;
    tbl.attachEvent("onmousemove",table_onMouseMove)
    tbl.attachEvent("onmouseout",table_onMouseOut)
   
    function table_onMouseMove() {
        var e = event.srcElement; 
        if (e.tagName == 'TD')
            e = e.parentElement;           
        if (e.tagName != 'TR') return;
//        if (e == _this.currentRow) return;       
//        if (_this.currentRow)
//            _this.currentRow.runtimeStyle.backgroundColor = '';
        e.runtimeStyle.backgroundColor = _this.hilightColor;
        _this.currentRow = e;
    }
   
    function table_onMouseOut() {
        var e = event.srcElement;
        if (e.tagName == 'TD')
            e = e.parentElement;           
        if (e.tagName != 'TR') return;
        var count=0;
        for(;count<_thisTable.rows.length;count++)
        {
            if(e==_thisTable.rows[count])
                break;
        }
        if(count%2 ==0)
            e.runtimeStyle.backgroundColor = 'EBF3FB';
        else
            e.runtimeStyle.backgroundColor = '';
    }

<script language="javascript" type="text/javascript">
// 调用范例,要写在页面的最下面,保证table已经产生,否则document.getElementById('table1')==null

var mytable = document.getElementById('table1');
var count=0;
for(;count<mytable.rows.length;count++)
{
    if(count%2 ==0)
        mytable.rows[count].runtimeStyle.backgroundColor = 'EBF3FB';
    else
        mytable.rows[count].runtimeStyle.backgroundColor = '';   
}
//调用所编辑的类,使得其响应鼠标事件。
var hilighter1 = new TableRowHilighter(document.getElementById('table1'), 1, '#FFDB77');

</script>

 

/************************************ANOTHER***************************************

function highlightRow(objRow)
    {
//        objRow.bgColor = "red";
        objRow.runtimeStyle.backgroundColor = "red";
    }
   
    function unhighlightRow(objRow)
    {
//        objRow.bgColor = "";
        objRow.runtimeStyle.backgroundColor = "";
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值