javascript table td row 的onmouseover 着色

<html>
    <head>
        <script>
            var lastindex = -1;
            var lastcolor;
            var buttoncheck = 0;
            var lastcellindex = -1;
            var tab;
            function wholerow(eve){
                even = eve.srcElement?eve.srcElement:eve.target;  /*因游览器有区别 所以需要判断 */
                tab = document.getElementById("table01");         /*也可为方法加入this参数  tab=this */
               
                if(buttoncheck == 0){                      /*选取整行的情况 */
                    if (even.tagName == "TD"){                        /*所选中的可能不是td */
                        if(lastindex != -1){                          /*对上次选中的td清色 */
                            tab.rows[lastindex].bgColor = lastcolor;
                        }
                        nowRow=even.parentNode;                       /*选择td的父节点TR */
                        lastindex=nowRow.rowIndex;                    /*为清色做准备 */
                        lastcolor=nowRow.bgColor;                     /*同上 */
                        tab.rows[nowRow.rowIndex].bgColor="#C9C8FF";  /*为鼠标所在TR着色 */
                    }
                }else{                                   /*选取单元格的情况 */
                    if(even.tagName == "TD"){
                        if((lastindex != -1)&&(lastcellindex != -1)){     /*对上次选中的td清色 */
                            tab.rows[lastindex].cells[lastcellindex].bgColor = lastcolor;  
                        }
                        lastcellindex = even.cellIndex;
                        nowRow=even.parentNode;
                        lastindex=nowRow.rowIndex;
                        lastcolor=nowRow.bgColor;
                       
                        tab.rows[lastindex].cells[lastcellindex].bgColor = "#C9C8FF";   
                    }
                }
            }
            function changefunction(){
                if(lastindex != -1){               /*button转换时清除color */
                    tab.rows[lastindex].bgColor = lastcolor;
                    tab.rows[lastindex].cells[lastcellindex].bgColor = lastcolor;
                }
               
                but_value = document.getElementById("but").value;
               
                if (but_value == "showonetd"){
                    document.getElementById("but").value = "showrow";   /*整行选取 */
                }else{
                    document.getElementById("but").value = "showonetd"; /*单元格选取 */
                }

                if (buttoncheck == 0){
                    buttoncheck = 1;   /*单元格选取 */
                }else{
                    buttoncheck = 0;   /*整行选取 */
                }
            }
        </script>
    </head>
    <body>
        <table border = '1' id = "table01" style = "width:30%" οnmοuseοver="wholerow(event);">
            <?php for ($i = 0;$i<10;$i++){?>
                <tr>
                    <td style = "width:30%;"><?php echo $i;?></td>
                    <td style = "width:40%;"><?php echo $i.$i.$i;?></td>
                    <td style = "width:30%;"><?php echo $i.$i.$i.$i.$i;?></td>
                </tr>
            <?php }?>
        </table>
        <input type = "button" id = "but" value = "showonetd" onclick = "changefunction();"></input>
    </body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值