前端小案例——输入框提示词(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

谢谢各位的支持~~

相关推荐
十一.3665 小时前
37-38 for循环
前端·javascript·html
波诺波5 小时前
环境管理器
linux·前端·python
San30.5 小时前
深入理解浏览器渲染流程:从HTML/CSS到像素的奇妙旅程
前端·css·html
IT_陈寒5 小时前
5个Python 3.12新特性让你的代码效率提升50%,第3个太实用了!
前端·人工智能·后端
周杰伦_Jay5 小时前
【Python Web开源框架】Django/Flask/FastAPI/Tornado/Pyramid
前端·python·开源
艾小码5 小时前
为什么你的JavaScript代码总是出bug?这5个隐藏陷阱太坑了!
前端·javascript
辻戋7 小时前
从零实现React Scheduler调度器
前端·react.js·前端框架
徐同保7 小时前
使用yarn@4.6.0装包,项目是react+vite搭建的,项目无法启动,报错:
前端·react.js·前端框架
Qrun8 小时前
Windows11安装nvm管理node多版本
前端·vscode·react.js·ajax·npm·html5
中国lanwp8 小时前
全局 npm config 与多环境配置
前端·npm·node.js