JS控制input颜色

需求:

         多个 input 标签(border为蓝色) ,某个input在获得焦点时,border的颜色发生改变(border为红色),失去焦点后border变回蓝色

效果图:

第一种使用className :

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
     <style type="text/css" media="all" title="Default">
         .onblur1 {            
            border:1px solid blue;
        }
        .onfocus1 {
           border:1px solid red;
        }
    </style>
    <script>
        window.onload = function ()
        {
            var obj = document.getElementsByTagName("input");
            //var obj = document.body.all.tags("input");
           // alert(obj.length);
            for (i = 0; i < obj.length; i++)
            {
                if (obj[i].type == "text")
                {
                    obj[i].className = "onblur1";
                    obj[i].onfocus = function () { this.className= "onfocus1"; }
                    obj[i].onblur = function () { this.className= "onblur1"; }                    
                }
            }
        }
    </script>   
</head>
<body>
    <input type="text" value="1" style="border-width:1px"/>
    <input type="text" value="2" style="border-width:1px"/>
    <input type="text" value="3" style="border-width:1px"/>
    <input type="text" value="4" style="border-width:1px"/>
    <input type="text" value="5" style="border-width:1px"/>
    <input type="text" value="8" style="border-width:1px"/>
</body>
</html>

第二种:使用cssText

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
     <style type="text/css" media="all" title="Default">
         /*.onblur1 {         
            border:1px solid blue;
        }
        .onfocus1 {
           border:1px solid red;
        }*/
    </style>
    <script>
        window.onload = function ()
        {
            var obj = document.getElementsByTagName("input");
            //var obj = document.body.all.tags("input");
           // alert(obj.length);
            for (i = 0; i < obj.length; i++)
            {
                if (obj[i].type == "text")
                {
                    obj[i].style.cssText = " border:1px solid blue";
                    obj[i].onfocus = function () { this.style.cssText= " border:1px solid red"; }
                    obj[i].onblur = function ()  { this.style.cssText= " border:1px solid blue"; }                    
                }
            }
        }
    </script>   
</head>
<body>
    <input type="text" value="1" style="border-width:1px"/>
    <input type="text" value="2" style="border-width:1px"/>
    <input type="text" value="3" style="border-width:1px"/>
    <input type="text" value="4" style="border-width:1px"/>
    <input type="text" value="5" style="border-width:1px"/>
    <input type="text" value="8" style="border-width:1px"/>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值