input输入密码类型,点击小眼睛显示密码或隐藏功能

时间: 2023-12-16 16:02:59 浏览: 68
是的,你可以使用JavaScript和CSS来实现一个可以切换密码可见性的功能。 首先,我们需要在HTML中添加一个input元素和一个小眼睛图标的按钮。在input元素中设置type为password,这会将input元素转换为密码输入框。 ```html <div class="input-group"> <input type="password" class="form-control" id="password-input" placeholder="Password"> <div class="input-group-append"> <button class="btn btn-outline-secondary" type="button" id="toggle-password"> <i class="fa fa-eye-slash"></i> </button> </div> </div> ``` 接下来,我们需要使用JavaScript来监听按钮的点击事件。当按钮被点击时,我们需要切换input元素的type属性,并根据type属性来切换按钮图标的class。 ```javascript const passwordInput = document.querySelector('#password-input'); const togglePasswordButton = document.querySelector('#toggle-password'); togglePasswordButton.addEventListener('click', function (e) { const type = passwordInput.getAttribute('type') === 'password' ? 'text' : 'password'; passwordInput.setAttribute('type', type); this.querySelector('i').classList.toggle('fa-eye'); this.querySelector('i').classList.toggle('fa-eye-slash'); }); ``` 最后,我们需要添加一些CSS样式来美化我们的密码输入框和按钮。 ```css .input-group { position: relative; } .input-group-append { position: absolute; right: 0; top: 0; height: 100%; } .btn { border-radius: 0; } .form-control { border-radius: 0; border-right: none; } .btn:focus, .form-control:focus { box-shadow: none; } .btn i { font-size: 1.2rem; } .fa-eye-slash:before { content: '\f070'; } .fa-eye:before { content: '\f06e'; } ``` 这样,我们就实现了一个可以切换密码可见性的功能。

相关推荐

最新推荐

recommend-type

JS实现表单中点击小眼睛显示隐藏密码框中的密码

主要介绍了JS实现表单中点击小眼睛显示隐藏密码框中的密码,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考价值,需要的朋友可以参考下
recommend-type

JS实现密码框的显示密码和隐藏密码功能示例

主要介绍了JS实现密码框的显示密码和隐藏密码功能,涉及javascript针对页面form表单元素动态操作的相关技巧,需要的朋友可以参考下
recommend-type

input禁止键盘及中文输入,但可以点击

主要介绍了&lt;input&gt;禁止键盘及中文输入,但可以点击。需要的朋友可以过来参考下,希望对大家有所帮助
recommend-type

jquery实现input输入框实时输入触发事件代码

input id=”productName” name=”productName” class=”wid10″ type=”text” value=”” /&gt; 代码如下: //绑定商品名称联想 $(‘#productName’).bind(‘input propertychange’, function() {...
recommend-type

JS中input表单隐藏域及其使用方法

主要介绍了JS中input表单隐藏域及其使用方法讲解,非常不错,具有参考借鉴价值,需要的朋友可以参考下
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

2. 通过python绘制y=e-xsin(2πx)图像

可以使用matplotlib库来绘制这个函数的图像。以下是一段示例代码: ```python import numpy as np import matplotlib.pyplot as plt def func(x): return np.exp(-x) * np.sin(2 * np.pi * x) x = np.linspace(0, 5, 500) y = func(x) plt.plot(x, y) plt.xlabel('x') plt.ylabel('y') plt.title('y = e^{-x} sin(2πx)') plt.show() ``` 运行这段
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。