前端小案例——输入框提示词(HTML+CSS+JS, 附源码)

一、前言

实现功能:

这个小案例实现了一个具有动态占位符的文本输入框。它会在输入框中循环显示一组预定义的占位符文本,并且每隔5秒钟就会更换一个占位符文本。这可以用于为用户提供一些提示或引导,让他们知道他们可以在输入框中输入什么内容。

实现逻辑:

  1. 首先,通过document.querySelector('input')获取到页面中的input元素。

  2. 定义了一个currentIndex变量,用于追踪当前占位符的索引。同时,定义了一个placeholders数组,其中包含了一组预定义的占位符文本。

  3. 调用updatePlaceholder函数来更新输入框的占位符文本。

  4. updatePlaceholder函数会将currentIndex对应的占位符文本赋值给输入框的placeholder属性,并将currentIndex自增1。如果currentIndex超过了placeholders数组的长度,则将其重置为0,实现循环显示占位符文本的效果。

  5. 使用setInterval函数每隔5秒钟调用一次updatePlaceholder函数,以更新占位符文本。

二、项目运行效果

三、全部代码

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;
            text-decoration: none;
            list-style: none;
            background-repeat: no-repeat;
        }
        .input-box{
            width: 500px;
            height: 50px;
            position: relative;
            left: 50%;
            transform: translate(-50%, 500%);
        }
        .input-box input{
            width: 100%;
            height: 100%;
            padding-left: 25px;
            box-sizing: border-box;
            border-radius: 25px;
            border: 2px solid #89ceb5;
            font-size: 18px;
            line-height: 50px;
        }
        .input-box input:focus { 
            border: 2px solid #5dbea4;
            outline: none; 
            caret-color: #65c4aa; 
        }
        .chakra-button {
            width: 30px;
            height: 30px;
            position: absolute;
            top: 52%;
            right: 3%;
            margin-top: -15px;
            background-color: transparent;
            color: #89ceb5;
            border: none;
            cursor: pointer;
            z-index: 22;
        }
    </style>
</head>
<body>
    <div class="input-box">
        <input type="text">
        <button type="button" class="chakra-button">
            <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 32 32"
                focusable="false" class="chakra-icon css-1cmji0l" aria-hidden="true">
                <path
                    d="M27.71 4.29a1 1 0 0 0-1.05-.23l-22 8a1 1 0 0 0 0 1.87l9.6 3.84l3.84 9.6a1 1 0 0 0 .9.63a1 
                    1 0 0 0 .92-.66l8-22a1 1 0 0 0-.21-1.05zM19 24.2l-2.79-7L21 12.41L19.59 11l-4.83 4.83L7.8 13l17.53-6.33z"
                    fill="currentColor">
                </path>
            </svg>
        </button>
    </div>
</body>
<script>
    const input = document.querySelector('input');
    let currentIndex = 0;
    let placeholders = ['欢迎来到「博主的文章」...', '1月推荐 | 盛冬时节...', '那些涤荡心灵的瞬间...'];
    updatePlaceholder();
    setInterval(updatePlaceholder, 5000);

    function updatePlaceholder() {
        input.placeholder = placeholders[currentIndex];
        currentIndex = (currentIndex + 1) % placeholders.length;
    }
</script>
</html>

四、答疑解惑

这是一个非常适合练习JavaScript的小案例,各位小伙伴可以自行更改样式和内容,如果大家运行时出现问题或代码有什么不懂的地方都可以随时评论留言或联系博主QQ。

还多请各位小伙伴多多点赞支持,你们的支持是我最大的动力。

博主QQ:1196094293

谢谢各位的支持~~

相关推荐
Lsx_9 分钟前
TypeScript 是怎么去查找类型定义的?
前端·javascript·typescript
xianxin_20 分钟前
CSS Dimension(尺寸)
前端
小宋搬砖第一名20 分钟前
前端包体积优化实战-从 352KB 到 7.45KB 的极致瘦身
前端
陈随易21 分钟前
前端之虎陈随易:2025年8月上旬总结分享
前端·后端·程序员
草巾冒小子25 分钟前
天地图应用篇:增加全屏、图层选择功能
前端
universe_0143 分钟前
day25|学习前端js
前端·笔记
Zuckjet1 小时前
V8 引擎的性能魔法:JSON 序列化的 2 倍速度提升之路
前端·chrome·v8
MrSkye1 小时前
🔥React 新手必看!useRef 竟然不能触发 onChange?原来是这个原因!
前端·react.js·面试
wayman_he_何大民1 小时前
初识机器学习算法 - AUM时间序列分析
前端·人工智能
juejin_cn1 小时前
前端使用模糊搜索fuse.js和拼音搜索pinyin-match提升搜索体验
前端