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>
相关推荐
子兮曰5 天前
jev-ultrafast 深度解析:7 秒订机票的浏览器 Agent 是如何炼成的
前端·后端·agent
子兮曰5 天前
Jev 爆发一周:7 秒 Agent 背后的 System One 生态与三场争议
前端·后端·ai编程
前端小万5 天前
写公众号赚了 3000 块后,我做了一款叫 "一键成稿" 的软件
前端·微信小程序
爱勇宝5 天前
ZCode 开源 24 小时:一份没有历史的账本,回答不了"有没有偷代码"
前端·后端·chatglm (智谱)
三十而立洋5 天前
Cookie 详解:从产生到安全,一次讲透
前端·javascript
卡布鲁5 天前
把一个 Vite + Vue3 应用塞进 qiankun (React + Umi3) 主站:十个坑的复盘
前端·javascript·react.js
李少兄5 天前
JavaScript 隐式全局变量解析
javascript
汉堡大王95275 天前
Jev:不是聊天机器人, 而是一个智能 if 语句
前端·人工智能·后端
梦想很大很大5 天前
从运行事实到回归证据:Workrun 的 Telemetry 与 Evaluation 实践
前端·人工智能·后端
计算机魔术师5 天前
Meta Muse agent 接入 Shopify 的 Shop Pay 实现代理式购物
前端