js操作元素练习题目(用户名的隐藏,开关灯效果,下拉菜单)

开关灯效果,

注意点:

  1. flag=1或者0区分两种对立的情况一种是关闭,另一种是打开

  1. 修改元素的样式属性通过element.style.属性名,这里属性名记得驼峰命名法

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div {
            width: 100%;
            height: 1200px;
            background-color: #fff;
        }
    </style>
</head>

<body>
    <!-- 开关灯效果 -->
    <div><button>开关灯</button></div>
    <script>
        var btn = document.querySelector('button');
        var div = document.querySelector('div');
        var flag = 0;
        btn.onclick = function () {
            if (flag == 0) {
                div.style.backgroundColor = 'black';
                flag = 1;
            } else {
                div.style.backgroundColor = 'white';
                flag = 0;
            }
        }
    </script>
</body>

</html>

用户名的隐藏

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=
    , initial-scale=1.0">
    <title>Document</title>
    <style>
        input {
            display: block;
            border: 13px solid rgb(134, 6, 55);
            margin: 10px auto;
            color: rgb(201, 118, 9);
        }
        .change {
            border: 5px solid rgb(19, 226, 64);
            color: #971493;
        }
    </style>
</head>
<body>
    <input type="text" value="邮箱" class="ipt">
    <input type="text" value="密码" class="psd">
    <script>
        // 获取元素
        var ipt = document.querySelector('.ipt');
        var psd = document.querySelector('.psd');
        // 注册事件,处理程序:获得焦点时:1没有输入信息,让文本框为空,改变文本框颜色,,2,输入信息时,用change, 失去焦点时:1没有输入信息,变为邮箱,不改变颜色2输入信息时,颜色变为黑色
        ipt.onfocus = function () {
            if (this.value !== '邮箱') {
                this.className = 'change';
            } else {
                this.value = '';
                this.className = 'change';
            }
        }
        ipt.onblur = function () {
            if (this.value === '') {
                this.value = '邮箱';
                this.className = 'ipt';
            } else {
                this.className = 'ipt';
                this.style.color = 'black';
            }
        }
        psd.onfocus = function () {
            if (this.value !== '密码') {
                this.className = 'change';
            } else {
                this.value = '';
                this.className = 'change';
            }
        }
        psd.onblur = function () {
            if (this.value === '') {
                this.value = '密码';
                this.className = 'psd';
            } else {
                this.className = 'psd';
                this.style.color = 'black';
            }
        }
    </script>
</body>

</html>

注意:

  1. 通过className修改多种样式属性

  1. 逻辑顺序

下拉菜单

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        button {
            position: relative;
            margin: 10px 20px;
            width: 100px;
        }
        ul {
            position: absolute;
            top: 42px;
            left: 29px;
            visibility: hidden;
            list-style-type: none;
            background-color: rgb(203, 60, 60);
            width: 100px;
            height: 200px;
        }
    </style>
</head>

<body>
    <!-- 下拉菜单 -->
    <button>网站导航 </button>
    <ul>
        <li>行业频道</li>
        <li>生活服务</li>
        <li>更多信息</li>
    </ul>
    <script>
        // 获取元素
        var btn = document.querySelector('button');
        var ul = document.querySelector('ul');
        // 注册事件,处理程序,当经过button时,ul出现
        btn.onmouseover = function () {
                ul.style.visibility = 'visible';
        }
       btn.onmouseout = function () {
                ul.style.visibility = 'hidden';
            }
    </script>
</body>

</html>

注意:

  1. 修改样式属性通过element.style.属性名

  1. 经过与离开

onmouseover

鼠标经过触发

onmouseout

鼠标离开触发

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值