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>
相关推荐
coding漫漫长路1 分钟前
img.decode()报错的图能画,load成功的却是空的
前端
不可能片场3 分钟前
AI视频首尾帧控制 用参考图定住镜头
前端·electron
修炼的dance8 分钟前
canvas最大能开多大:先看面积,再看导出格式
前端
付威20238 分钟前
Rust 所有权:看你的答对几道题?
javascript
西红柿炖牛腩35416 分钟前
Three.js 模型体积优化:Draco/Meshopt 压缩与 DRACOLoader 配置的 4 个步骤
javascript·3d·性能优化
薛一半20 分钟前
React-Redux核心原理与容器组件实战
前端·javascript·react.js
做运维的阿瑞20 分钟前
自连接、交叉连接与复杂 JOIN:一条问题链讲透
前端·数据库·mysql
web打印社区30 分钟前
C-Lodop 提示未准备好或 WebSocket 没准备好:先让本机服务起来
javascript·网络·websocket·网络协议·pdf·html
言乐631 分钟前
JavaScript概括前端原理
开发语言·前端·javascript·python·ecmascript
回眸&啤酒鸭1 小时前
DeepSeek V4.1 Flash 批量处理实战指南
服务器·前端·javascript