HTML集成优雅的实时输入清除功能

引言

你好呀,我是小邹。

点击访问 我的个人博客

在现代网页设计中,用户体验是至关重要的。一个流畅、直观且反应迅速的界面能够显著提升用户满意度。本文将介绍如何在网页表单中集成实时输入清除功能,即在输入框中显示一个"x"图标,允许用户一键清除输入框内的文本。我们将使用HTML、CSS和JavaScript来实现这一功能,同时保持界面的简洁和美观。

效果图

HTML结构

我们的示例代码基于一个标准的输入框,我们将在输入框右侧嵌入一个清除图标。这里是HTML代码的基本结构:

html 复制代码
<div class="field m-mobile-wide m-margin-bottom-small" style="padding-top: 10px">
    <div class="ui left icon input">
        <i class="qq icon"></i>
        <input type="text" id="QQ" name="qq" placeholder="输入QQ号自动获取昵称头像" required="required">
        <span class="clear-input" style="display: none;">
            <i class="fa fa-times-circle"></i>
        </span>
    </div>
</div>

这里我们使用了<span>元素来包裹清除图标,初始时设置display: none;使其不可见。

CSS样式

为了使清除图标位于输入框的右侧,我们需要使用CSS来定位和美化这个图标。以下是CSS代码:

css 复制代码
.clear-input {
    position: absolute;
    right: 0.5rem;
    top: 50%;
    transform: translateY(-50%);
    cursor: pointer;
    display: none; /* 默认隐藏 */
}

这段代码将清除图标定位在输入框的右上角,并通过transform属性垂直居中。

JavaScript逻辑

为了让清除图标在输入框有文本时显示,并在点击时清除文本,我们需要使用JavaScript来添加事件监听器。下面是JavaScript代码:

javascript 复制代码
document.addEventListener('DOMContentLoaded', function () {
    const inputs = document.querySelectorAll('.ui.left.icon.input input');
    const clearSpans = document.querySelectorAll('.ui.left.icon.input .clear-input');
    
    inputs.forEach((input, index) => {
        // 检查输入框是否非空,如果是,则显示清除图标
        if (input.value) {
            clearSpans[index].style.display = 'block';
        }
        
        // 当输入框内容改变时,显示或隐藏清除图标
        input.addEventListener('input', function () {
            if (this.value) {
                clearSpans[index].style.display = 'block';
            } else {
                clearSpans[index].style.display = 'none';
            }
        });
        
        // 当输入框获得焦点时,如果已有内容,显示清除图标
        input.addEventListener('focus', function () {
            if (this.value) {
                clearSpans[index].style.display = 'block';
            }
        });
        
        // 当输入框失去焦点且为空时,隐藏清除图标
        input.addEventListener('blur', function () {
            if (!this.value) {
                clearSpans[index].style.display = 'none';
            }
        });
        
        // 当点击清除图标时,清空输入框并隐藏图标
        clearSpans[index].addEventListener('click', function (event) {
            event.stopPropagation();
            input.value = '';
            clearSpans[index].style.display = 'none';
        });
    });
});

这段代码监听了input、focus和blur事件,根据输入框的状态显示或隐藏清除图标。当清除图标被点击时,会清空输入框并再次隐藏图标。

总结

通过上述步骤,我们可以轻松地在网页表单中实现一个实用且美观的实时输入清除功能。这不仅提高了用户交互的便利性,也增强了整体的用户体验。希望这篇文章能帮助你优化自己的网站或应用程序的用户界面设计。

相关推荐
步行cgn5 小时前
Axios 请求成功但页面不显示:两个前端错误的分析与修正
前端
时间的情敌6 小时前
WebGIS + OpenLayers + GeoServer + PostGIS #量算 #点聚合
前端·geoserver·openlayers
不可能片场6 小时前
Electron 发布 cookie 库先查明文再写解密
前端·electron
郭小李7 小时前
springmvc 拦截器
java·开发语言·前端
web打印社区7 小时前
Chrome-kiosk-printing能上生产吗
开发语言·前端·javascript·chrome·websocket
白帽攻防录7 小时前
某pc客户端app从xss到rce实战案例
前端·网络安全·黑客·xss·rce·src漏洞·app漏洞
志尊宝7 小时前
AI原生前端开发(041):AI辅助的状态管理(Zustand/Pinia)
前端·javascript·ai-native
修炼的dance7 小时前
pngquant 两档质量后接 oxipng,每步省多少花几秒
前端
费曼学习法7 小时前
无人值守AI内容矩阵实战(10):Windows 无 systemd,进程存活率 99% 怎么做
前端
小兔子7 小时前
Integer 的 == 在 100 时是 true,在 1000 时是 false
前端