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>
相关推荐
御坂100272 分钟前
Vue - @change应用实现下拉框联动功能
前端·javascript·vue.js
小雨下雨的雨3 分钟前
基于 Electron 运行时的鸿蒙PC桌面应用-安全可靠的随机密码生成工具
前端·javascript·华为·electron·前端框架·鸿蒙
瘦瘦瘦大人5 分钟前
Vue 项目实现关闭/刷新浏览器窗口前的离开确认提示
前端·javascript·vue.js
大家的林语冰6 分钟前
尤雨溪官宣:Vite+ 全员加盟 Cloudflare,正式进军全栈开发和 AI 部署云平台!
前端·javascript·vite
独特的螺狮粉15 分钟前
金属硬度与熔点对照表APP - 通过鸿蒙PC Electron框架完整技术实现指南
前端·javascript·electron·前端框架·开源·鸿蒙
Java_2017_csdn34 分钟前
在 Java 中,MessageFormat.format() 和 String.format() 函数对比?
java·开发语言·前端·数据库
IT策士35 分钟前
第 44篇 k8s之实战:将 Web 应用迁移到 Kubernetes(上)
前端·容器·kubernetes
用户059540174461 小时前
把Agent记忆测试从Mock换到真实Redis,漏测率从30%降到0
前端·css
Surprisec1 小时前
如何用 TypeScript 写一个最小可运行的 CLI Agent
前端·人工智能·typescript
marskim1 小时前
零依赖、高性能!从零实现 React 拖拽排序组件(基于 HTML5 Drag and Drop API)
前端