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

谢谢各位的支持~~

相关推荐
小二·6 小时前
Python Web 开发进阶实战 :AI 原生数字孪生 —— 在 Flask + Three.js 中构建物理世界实时仿真与优化平台
前端·人工智能·python
Whisper_Sy6 小时前
Flutter for OpenHarmony移动数据使用监管助手App实战 - 网络状态实现
android·java·开发语言·javascript·网络·flutter·php
新缸中之脑7 小时前
Weave.js:开源实时白板库
开发语言·javascript·开源
Amumu121387 小时前
Vue组件化编程
前端·javascript·vue.js
We་ct7 小时前
LeetCode 6. Z 字形变换:两种解法深度解析与优化
前端·算法·leetcode·typescript
小二·8 小时前
Python Web 开发进阶实战(终章):从单体应用到 AI 原生生态 —— 45 篇技术演进全景与未来开发者生存指南
前端·人工智能·python
m0_637256588 小时前
vue-baidu-map添加了类型组件导致非常卡顿的问题
前端·javascript·vue.js
雨季6668 小时前
基于设备特征的响应式 UI 构建:Flutter for OpenHarmony 中的智能布局实践
javascript·flutter·ui
挂机且五杀8 小时前
为什么在React地图组件里,memo 不是优化,而是生存?
前端·react.js·前端框架
RFCEO8 小时前
HTML编程 课程七、:HTML5 新增表单标签与属性
前端·html·html5·搜索框·手机号·邮箱验证·日期选择