JS:事件监听学习笔记以及案例:关掉广告

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

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>事件监听</title>
  <style>
    .box {
      background-color: pink;
      font-size: 50px;
      line-height: 200px;
      font-weight: 700;
    }

    .box1 {
      position: absolute;
      right: 20px;
      top: 10px;
      width: 20px;
      height: 20px;
      background-color: skyblue;
      text-align: center;
      line-height: 20px;
      font-size: 16px;
      cursor: pointer;//鼠标变小手
    }
  </style>
</head>

<body>
  <!-- 事件:在编程时系统内发生的事件
    事件监听:事件被触发会作出响应
   -->
  <!-- <button>xx</button> -->
  <div class="box">
    是的,我是广告位,目前在招租
    <div class="box1">
      x
    </div>
  </div>


  <script>
    //语法:元素对象.addEventlistener('事件类型',要执行的函数),函数不会立即执行,等事件触发才会执行
    // 事件监听三要素:
    // 事件源:哪个dom元素被事件触发了,要获取dom元素;
    // 事件类型:用什么方式触发,例如鼠标点击;
    // 事件调用的函数:要做什么事

    // 需求:点击按钮弹出一个对话框
    // 事件源
    // const btn = document.querySelector('button')
    // btn.addEventListener('click', function () {
    //   alert('zaozaozao')
    // })//click鼠标点击事件

    // 大盒子关闭小盒子也会被关闭
    const box1 = document.querySelector('.box1')
    const box = document.querySelector('.box')
    // 事件侦听
    box1.addEventListener('click', function () {
      box.style.display = 'none'
    })


  </script>


</body>

</html>

案例结果 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值