webapi01 -练习

这篇博客介绍了使用WebAPI实现的三个功能:微博下拉菜单,展示了一个动态效果的结果;用户名显示隐藏内容,通过交互控制用户名的可见性;以及开关灯效果,展示了如何用JavaScript控制元素的视觉状态。每个功能都有对应的截图展示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

webapi01
有错望指正!!!

微博下拉菜单

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            list-style: none;
            padding: 0;
            margin: 0;
            box-sizing: border-box;
        }
        
        .nav {
            width: 80px;
        }
        
        .tab {
            padding: 10px 0 10px;
            text-align: center;
        }
        
        .selector {
            display: none;
        }
        
        .selector ul {
            border: 1px solid orange;
            border-top: none;
            border-bottom: none;
        }
        
        .selector li {
            padding: 5px 20px;
            border-bottom: 1px solid orange;
        }
        
        .arrow {
            display: inline-block;
            width: 8px;
            height: 5px;
            margin: 0 0 0 5px;
            vertical-align: middle;
            background: url(icon.png) 0 -977px no-repeat;
        }
    </style>
</head>

<body>
    <div class="nav">
        <div class="tab">
            <i>微博</i>
            <em class="arrow"> </em>
        </div>
        <div class="selector">
            <ul>
                <li>评论</li>
                <li>私信</li>
                <li>@我</li>
            </ul>
        </div>
    </div>
    <script>
        // 获取事件源
        var nav = document.querySelector('.nav');
        console.dir(nav);
        var tab = document.querySelector('.tab');
        var sel = document.querySelector('.selector');
        var lis = document.querySelectorAll('li');
        console.log(lis);
        // 绑定事件 事件程序
        nav.onmouseover = function() {
            tab.style.backgroundColor = '#ccc';
            sel.style.display = 'block';
        }
        nav.onmouseout = function() {
            tab.style.backgroundColor = '';
            sel.style.display = 'none';
        }
        tab.onmouseover = function() {
            this.style.color = '#ff8400';
        }
        tab.onmouseout = function() {
            this.style.color = '#4c4c4c';
        }
        for (var i = 0; i < lis.length; i++) {
            lis[i].onmouseover = function() {
                this.style.color = '#ff8400';
                this.style.backgroundColor = '#fff5da';
            }
            lis[i].onmouseout = function() {
                this.style.color = '#4c4c4c';
                this.style.backgroundColor = '';
            }
        }
    </script>
</body>

</html>

结果图
在这里插入图片描述

用户名显示隐藏内容


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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        input {
            width: 200px;
            height: 30px;
            border: 1px solid #cccccc;
            outline: none;
        }
    </style>
</head>

<body>
    <input type="text" value="邮箱/ID/手机号">
    <script>
        // 获取事件源
        var input = document.querySelector('input');
        // 绑定事件 事件程序
        //onfocus 得到焦点  onblur 失去焦点
        input.onfocus = function() {
            input.value = '';
            input.style.borderColor = 'pink';
        }
        input.onblur = function() {
            input.value = '邮箱/ID/手机号';
            input.style.borderColor = '#ccc';
        }
    </script>
</body>

</html>

结果图
在这里插入图片描述

开关灯


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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        body {
            background-color: #fff;
        }
    </style>
</head>

<body>
    <button>开关灯</button>
    <script>
        var btn = document.querySelector('button');
        var flag = 0; //默认关着
        btn.onclick = function() {
            if (flag == 0) {

                document.body.style.backgroundColor = 'black';
                flag = 1;
            } else {
                document.body.style.backgroundColor = '#fff';
                flag = 0;
            }
        }
    </script>
</body>

</html>

结果图
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值