jsweb2

事件监听:程序检测是否有事件产生,一旦有事件触发,就立即调用一个函数做出响应

  1. 语法: 元素对象.addEventListener('事件类型',要执行的函数)

```html

<button class="btn">按钮</button>

<script>

const btn = document.querySelector('.btn')

btn.addEventListener('click',function(){

alert('点击l')

})

</script>

```

  1. 举例:按钮广告事件
html 复制代码
    <style>

        .adv{

            background-color: brown;

            position: relative;

            top:100px;

            left:500px;

            text-align: center;

        }

        img{

            width: 500px;

        }

        #close{

            width: 20px;

            height: 20px;

            background-color: white;

            position: absolute;

            top:10px;

            right:10px;

            cursor:pointer;

        }

        .hide{

            display: none;

        }

        .show{

            display: inline-block;

        }

    </style>

<body>

    <p>真传奇,和渣渣辉一起一刀999</p>

    <button id="btn">马上下载</button>

    <!-- 广告 -->

    <div id="adv" class="adv hide">

        <div id="close">X</div>

        <img src="../images/slider01.jpg" alt=""><br/>

        <h3>双十一跳楼大甩卖</h3>

    </div>

    <!-- 游戏注册 -->

    <div class="register hide">

        <h1>用户注册</h1>

        <form action="">

            输入手机号:<input type="text" name="uname"><button class="btn">提交</button>

        </form>

    </div>  

    <script>

        const bt = document.querySelector('#btn')

        const xx = document.querySelector('#close')

        const adv = document.querySelector('.adv')

        const register=document.querySelector('.register')

        bt.addEventListener('click',function(){

            adv.classList.remove('hide')

            adv.classList.add('show')

        })

        xx.addEventListener('click',function(){

            adv.classList.remove('show')

            adv.classList.add('hide')

            register.classList.remove('hide')

            register.classList.add('show')

        })

    </script>
  1. 事件类型
  • 鼠标事件(鼠标触发)

  • click 鼠标点击

  • mouseenter 鼠标经过

  • mouseleave 鼠标离开

  1. 随机点名
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>
        .suiji{
            width: 300px;
            height: 150px;
            background-color: rgb(255, 255, 255);
            margin: 0 auto;
            font-size: 20px;
            font-weight: bold;
            /* text-align: center; */
        }
        h2{
            text-align: center;
        }
        button{
            width: 80px;
            height: 30px;
        }
        .btn{
            text-align: center;
        }
    </style>
</head>
<body>
    <div class="suiji">
        <h2>随机点名</h2>
        <p>问题是:<span>这里显示人名</span></p>
        <div class="btn">
            <button id="action">开始</button>
            <button id="over">结束</button>
        </div>
    </div>
    <script>
        const name=['张三','李四','王五','赵六','小七','老八','九九']
        // 获取
        const sp=document.querySelector('span')
        const action=document.querySelector('#action')
        const over=document.querySelector('#over')
        let time=0
        let num=0
        // 点击开始按钮
        action.addEventListener('click',function(){
            time=setInterval(function(){
                num=Math.floor(Math.random()*name.length)
                sp.innerHTML=name[num]  
            },100)     
            if (name.length===1) {
                action.disabled=true
                over.disabled=true
            }
        })
        // 点击结束按钮
        over.addEventListener('click',function(){
                clearInterval(time)
                name.splice(num,1)
                // console.log(name)   打印删除后的名单数组
            })
        
        
    </script>
</body>
</html>
相关推荐
小羊没烦恼!2 天前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
三十而立洋2 天前
Cookie 详解:从产生到安全,一次讲透
前端·javascript
伞伞悦读2 天前
【第38期】Python 模块与包详解:import、from、模块搜索路径、包结构和 __init__
开发语言·python
卡布鲁2 天前
把一个 Vite + Vue3 应用塞进 qiankun (React + Umi3) 主站:十个坑的复盘
前端·javascript·react.js
C语言小火车2 天前
C/C++ 为什么需要编译器?
开发语言·c++
李少兄2 天前
JavaScript 隐式全局变量解析
javascript
霍霍的袁2 天前
【C++】map 和 set 的使用 | 从用法到底层
开发语言·c++·学习·visual studio
孙启超2 天前
【AI开发之Rust】第 11 课:智能指针与内部可变性
开发语言·后端·rust
此生决int2 天前
深入理解C++系列(20)——C++11(下)
开发语言·c++
沙蒿同学2 天前
我用 Go 搭了一条 AI Agent 流水线:从 1 张商品图到一整套淘宝详情页
前端·javascript·后端