当前输入框高亮显示

输入框高亮

HTML

<form class="container">
    <h2>用户登录</h2>
    <div class="content">
        <!--<p><label>用户名:</label><input class="f-text" type="text"></p>-->
        <span style="margin-right: 2px; margin-bottom: 10px">用户名:</span>
        <input class="f-origin" type="text" style="margin-bottom: 10px"><br>
        <span style="margin-right: 3px;margin-left: 10px">密码:</span>
        <input class="f-origin" type="password">
    </div>
    <button class="login">登录</button>
</form>

CSS

.container {
            width: 400px;
            margin:0 auto;
            background-color: rgb(254, 244, 235);
            border: 1px solid orangered;
        }
        h2 {
            //h2不应该设置width,一旦margin改变,h2就会超出父元素或留白
            //width: 392px;
            margin: 0;
            color: orangered;
            background-color: rgb(255, 235, 215);
            padding: 3px;
            border-bottom: 1px solid orangered;
        }
        .content {
            width: 220px;
            margin: 20px auto;
        }
        .f-origin {
            background-color: rgb(255,253,236);
            border-radius: 3px;
        }
        .login {
            width: 87px;
            background-color: orangered;
            padding: 5px 30px;
            border-radius: 3px;
            border: none;
            color: #ffffff;
            margin-bottom: 10px;
            text-align: center;
            margin-left: 132px;
        }
        .f-text {
            background-color: rgb(255,231,231);
        }

JS

window.onload = function () {
        var aInput = document.getElementsByTagName('input');
        for (var i=0; i<aInput.length; i++) {
            aInput[i].onfocus = function () {
                this.className = 'f-text';
            }
            aInput[i].onblur = function () {
                this.className = 'f-origin';
            }
        }
    }

onfocus当元素获得焦点时触发
onblur当元素失去焦点时触发
className用于js中改变类名, 在之前的实例中使样式为默认样式就应该用这个,一直知道这个属性,竟然没想到

在其他人的代码中, 用户名,密码以及登录按钮的HTML都是这样

<p><label>用户名</label><input type="text" class="f-text" /></p>

按钮也是这样,只是type不同, <label></label>没有值也写在里面, 设置label宽度, 所以按钮与输入框左对齐, 循环所有tagName为input的元素, 当按钮元素获得焦点时也可以改变按钮的背景色

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 一、JS前言 (1)认识JS 也许你已经了解HTML标记(也称为结构),知道了CSS样式(也称为表示),会使用HT...
    凛0_0阅读 7,751评论 0 8
  • HTML标签解释大全 一、HTML标记 标签:!DOCTYPE 说明:指定了 HTML 文档遵循的文档类型定义(D...
    米塔塔阅读 8,676评论 1 41
  • 问答题47 /72 常见浏览器兼容性问题与解决方案? 参考答案 (1)浏览器兼容问题一:不同浏览器的标签默认的外补...
    _Yfling阅读 14,687评论 1 92
  • 这个故事发生在王猛的大伯烧头七的那天晚上,自那之前王猛不相信任何鬼神之说,他觉得任何事情都有科学依据,比如诈尸吧,...
    训诸相恋盛叙狗柳井幻园阅读 3,017评论 0 0
  • 幼儿教育是学前教育或说早期教育的后半阶段,前面与o~3岁的婴儿教育衔接,后面与初等教育衔接,是一个人教育与发展...
    惘囚_793e阅读 2,405评论 0 0