javascript控制GridView隔行换色、鼠标移动变色,选中变色

本文介绍了如何使用JavaScript实现GridView的行颜色控制,包括隔行换色、鼠标悬停变色和选中变色。提供了两种不同的颜色设置方案,并特别处理了包含CheckBox时的行颜色变化。

 <script type="text/javascript">
        //把事件放在onload里,因为我不知道JS如果直接写到这儿是不是会等页面加载完才执行
        //使用&lt;%=%>方式输出GridView的ID是因为某些情况下(如使用了MasterPage)会造成HTML中ID的变化
        //颜色值推荐使用Hex,如 #f00 或 #ff0000
        window.onload = function(){
            GridViewColor("<%=GridView.ClientID%>","#fff","#eee","#6df","#fd6");
        }
        
        //参数依次为(后两个如果指定为空值,则不会发生相应的事件):
        //GridView ID, 正常行背景色,交替行背景色,鼠标指向行背景色,鼠标点击后背景色
        function GridViewColor(GridViewId, NormalColor, AlterColor, HoverColor, SelectColor){
            //获取所有要控制的行
            var AllRows = document.getElementById(GridViewId).getElementsByTagName("tr");
            
            //设置每一行的背景色和事件,循环从1开始而非0,可以避开表头那一行
            for(var i=1; i<AllRows.length; i++){
                //设定本行默认的背景色
                AllRows[i].style.background = i%2==0?NormalColor:AlterColor;
                
                //如果指定了鼠标指向的背景色,则添加onmouseover/onmouseout事件
                //处于选中状态的行发生这两个事件时不改变颜色
                if(HoverColor != ""){
                    AllRows[i].onmouseover = function(){if(!this.selected)this.style.background = HoverColor;}
                    if(i%2 == 0){
                        AllRows[i].onmouseout = function(){if(!this.selected)this.style.background = NormalColor;}
                    }
                    else{
                        AllRows[i].onmouseout = function(){if(!this.selected)this.style.background = AlterColor;}
                    }
                }
                
                //如果指定了鼠标点击的背景色,则添加onclick事件
                //在事件响应中修改被点击行的选中状态
                if(SelectColor != ""){
                    AllRows[i].onclick = function(){
                        this.style.background = this.style.background==SelectColor?HoverColor:SelectColor;
                        this.selected = !this.selected;
                    }
                }
            }
        }
    </script>

 

 

 

 

假如GridView里有CheckBox 选中CheckBox变色

 

 

 <script type="text/javascript">

        //把事件放在onload里,因为我不知道JS如果直接写到这儿是不是会等页面加载完才执行
        //使用&lt;%=%>方式输出GridView的ID是因为某些情况下(如使用了MasterPage)会造成HTML中ID的变化
        //颜色值推荐使用Hex,如 #f00 或 #ff0000
        window.onload = function(){
            GridViewColor("<%=GridView1.ClientID%>","#f7f6f3","#edf1f8","#d8e7b0","#d7e3f6");
        }
       
        //参数依次为(后两个如果指定为空值,则不会发生相应的事件):
        //GridView ID, 正常行背景色,交替行背景色,鼠标指向行背景色,鼠标点击后背景色
        function GridViewColor(GridViewId, NormalColor, AlterColor, HoverColor, SelectColor){
            //获取所有要控制的行
            var AllRows = document.getElementById(GridViewId).getElementsByTagName("tr");

            //全选行变色

            AllRows[0].getElementsByTagName("INPUT")[0].onclick = function()
            {
               
                for(var i=1; i<AllRows.length; i++)
                {
                   
                    AllRows[i].getElementsByTagName('input')[0].checked=AllRows[0].getElementsByTagName("INPUT")[0].checked;
                    if(AllRows[0].getElementsByTagName("INPUT")[0].checked)
                    {  
                        AllRows[i].style.background=SelectColor;
                    }
                    else
                    {
                        if(i%2 == 0){AllRows[i].style.background=AlterColor;}
                        else{AllRows[i].style.background=NormalColor;}
                    }
                }
            }

 

 

            //设置每一行的背景色和事件,循环从1开始而非0,可以避开表头那一行
            for(var i=1; i<AllRows.length; i++){
                //设定本行默认的背景色
                AllRows[i].style.background = i%2==0?AlterColor:NormalColor;
               
                //如果指定了鼠标指向的背景色,则添加onmouseover/onmouseout事件
                //处于选中状态的行发生这两个事件时不改变颜色
                if(HoverColor != ""){
                    AllRows[i].onmouseover = function(){if(!this.getElementsByTagName("INPUT")[0].checked)this.style.background = HoverColor;}
                    if(i%2 == 0){
                        AllRows[i].onmouseout = function(){if(!this.getElementsByTagName("INPUT")[0].checked)this.style.background =AlterColor ;}
                    }
                    else{
                        AllRows[i].onmouseout = function(){if(!this.getElementsByTagName("INPUT")[0].checked)this.style.background = NormalColor;}
                    }
                }
               
                //如果指定了鼠标点击的背景色,则添加onclick事件
                //在事件响应中修改被点击行的选中状态
                if(SelectColor != ""){
                    AllRows[i].getElementsByTagName("INPUT")[0].onclick = function(){
                        this.parentNode.parentNode.style.background =      this.parentNode.parentNode.style.background==SelectColor?HoverColor:SelectColor;

                    }
                }
            }
        }

   
    </script>

 

 

 

后台可实现点击CheckBox 全选和行变色

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        if (e.Row.RowType == DataControlRowType.Header)
        {
            CheckBox cb = e.Row.FindControl("CheckBox2") as CheckBox;
            cb.Attributes.Add("onclick", "var GridView1=document.getElementById('GridView1');for(var i=1; i<GridView1.rows.length;i++){GridView1.rows[i].cells[0].getElementsByTagName('input')[0].checked=checked;}");
        }

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            CheckBox cb = e.Row.FindControl("CheckBox1") as CheckBox;
            cb.Attributes.Add("onclick", "if(checked){this.parentNode.parentNode.style.backgroundColor='#d7e3f6';}else{this.parentNode.parentNode.style.backgroundColor='';}");
        }   
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值