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>
相关推荐
訾博ZiBo12 分钟前
React 状态管理中的循环更新陷阱与解决方案
前端
StarPrayers.26 分钟前
旅行商问题(TSP)(2)(heuristics.py)(TSP 的两种贪心启发式算法实现)
前端·人工智能·python·算法·pycharm·启发式算法
一壶浊酒..44 分钟前
ajax局部更新
前端·ajax·okhttp
苏打水com1 小时前
JavaScript 面试题标准答案模板(对应前文核心考点)
javascript·面试
Wx-bishekaifayuan1 小时前
基于微信小程序的社区图书共享平台设计与实现 计算机毕业设计源码44991
javascript·vue.js·windows·mysql·pycharm·tomcat·php
DoraBigHead2 小时前
React 架构重生记:从递归地狱到时间切片
前端·javascript·react.js
ะัี潪ิื2 小时前
精灵图(雪碧图)的生成和使用
java·css
彩旗工作室2 小时前
WordPress 本地开发环境完全指南:从零开始理解 Local by Flywhee
前端·wordpress·网站
iuuia2 小时前
02--CSS基础
前端·css
kyle~2 小时前
Qt---setAttribute设置控件或窗口的内部属性
服务器·前端·c++·qt