动态操作CSS文本框失去焦点变颜色(注册onblur事件)

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/qq_32077521/article/details/97615601
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
	<meta charset="utf-8" />
</head>
<body>
    <input /><input /><input /><input /><input /><input /><input />
    <script type="text/javascript">
        window.onload = function () {
            var txtObjs = document.getElementsByTagName('input');
            for (var i = 0; i < txtObjs.length; i++) {
                if (txtObjs[i].type == 'text') {
                    //注册失去焦点的事件
                    txtObjs[i].onblur = function () {
                        if (this.value.length == 0) {
                            this.style.backgroundColor = 'red';
                        }
                        else {
                            this.style.backgroundColor = 'white';
                        }
                    };
                }
            }
        };
    </script>
</body>
</html>

猜你喜欢

转载自blog.csdn.net/qq_32077521/article/details/97615601