JavaScript_11_练习:小米搜索框案例(焦点事件)

效果图

代码

html 复制代码
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>练习:小米搜索框案例(焦点事件)</title>
  <style>
    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }

    li {
      list-style: none;
    }

    .box {
      margin: 50px auto;
      width: 223px;
    }

    .box input {
      padding: 0 10px;
      width: 223px;
      height: 48px;
      border: 1px solid #e0e0e0;
      /* 去除元素在获得焦点时显示的轮廓线,
      否则看不出获得焦点时的橙色边框线效果 */
      outline: none;
    }

    /* 获得焦点 */
    .box input:focus {
      border: 1px solid #ff6700;
    }

    .box ul {
      display: none;
      border: 1px solid #ff6700;
      border-top: none;
    }

    .box ul li a {
      display: block;
      padding: 6px 15px;
      font-size: 12px;
      color: #424242;
      text-decoration: none;
    }

    .box ul li a:hover {
      background-color: #eeeeee;
    }
  </style>
</head>

<body>
  <div class="box">
    <input type="search" placeholder="小米11">
    <ul>
      <li><a href="#">全部商品</a></li>
      <li><a href="#">小米11</a></li>
      <li><a href="#">小米10S</a></li>
      <li><a href="#">小米笔记本</a></li>
      <li><a href="#">小米手机</a></li>
      <li><a href="#">黑鲨4</a></li>
      <li><a href="#">空调</a></li>
    </ul>
  </div>
  <script>
    const input = document.querySelector(".box input")
    const ul = document.querySelector(".box ul")
    // 获得焦点
    input.addEventListener("focus", () => {
      ul.style.display = "block"
    })
    // 失去焦点
    input.addEventListener("blur", () => {
      ul.style.display = "none"
    })
  </script>
</body>

</html>
相关推荐
To_OC24 分钟前
面试被问了三回三栏布局,这次我终于把 BFC 那层窗户纸捅破了
前端·css·面试
To_OC1 小时前
啃完 TS 工具类型我发现:Pick 和 Omit 原来就是一层窗户纸
前端·面试·typescript
风月说与山鬼2 小时前
三、大括号语法
前端·react.js
kyriewen2 小时前
我把最常踩的8个CORS跨域报错整理了一遍——第8个去年还不存在
前端·javascript
Csvn3 小时前
🕰️ 闭包 + setTimeout 的 5 个经典陷阱:为什么定时器看到的永远不是最新的值?
前端
lilian2333 小时前
Harmony os 技术实战|拼豆制图27:用单字符编码承载 50 张 70×70 图纸
前端·数据库·华为·harmonyos
CarIise4 小时前
CSS选择器与样式关联
前端·css·tensorflow
里欧跑得慢5 小时前
CSS 模块化架构的演进:BEM、CSS Modules 到 CSS-in-JS 的反思
前端·css·flutter·web·css-in-js
IT_陈寒5 小时前
Vue的computed属性把我坑惨了,原来我一直用错姿势
前端·人工智能·后端