input密码框获取焦点,不出现账号密码下拉列表

最近有一个项目表单中有密码框,但是在浏览器上获取input焦点后就会出现之前记住的账号密码信息,产品要求不需要显示这个下拉框。
翻阅了大量的资料并没有好的解决方案;
比较详细的参考链接:https://blog.csdn.net/weixin_43043994/article/details/97890856

项目是基于vue2+elementui框架下开发的
以下提供个人觉得更好的思路与代码
大致思路:
当输入框有值时input设置为password类型,没有值或者清空值时重新渲染input框为text类型。
详细:调用el-input组件,配置show-password项;
加载前(created方法)先先判断密码框是否有数据,如果有则isShowPwd设置为true,如果没有则设置为false
监听input事件,当值为空时设置input框类型为text,show-password设置为false,同时改变key值,让input框重新渲染然后获取焦点;如果值不为空则show-password设置为true

<el-input
    :key="pwdKey"
    ref="pwd-input"
    size="default"
    :type="pwdType"
    v-model="value"
    placeholder="请输入密码"
    @input="pwdInput"
/>


data(){
    value: '',
    pwdType: 'text',
    pwdKey: 0
	iptKey:0
},
methods: {
	pwdInput(e) {
      if(e){
        this.pwdType = 'password';
      } else {
        this.pwdKey ++;
        this.pwdType = 'text';
        this.$nextTick(() => {
          this.$refs['pwd-input'].focus();
        });
      }
    }
}

缺点:当值为空时会重新渲染input,会快速闪一下

input 密码获取焦点时,我们可以通过添加一个小眼睛图标,来实现展示密码的功能。以下是一种实现方式: HTML代码: ```html <label for="password">密码:</label> <div class="password-wrapper"> <input type="password" id="password" name="password"> <i class="password-toggle-icon fas fa-eye"></i> </div> ``` CSS代码: ```css .password-wrapper { position: relative; } .password-toggle-icon { position: absolute; top: 50%; right: 10px; transform: translateY(-50%); cursor: pointer; } ``` JavaScript代码: ```javascript const passwordInput = document.getElementById("password"); const passwordToggleIcon = document.querySelector(".password-toggle-icon"); passwordToggleIcon.addEventListener("click", function() { if (passwordInput.type === "password") { passwordInput.type = "text"; passwordToggleIcon.classList.remove("fa-eye"); passwordToggleIcon.classList.add("fa-eye-slash"); } else { passwordInput.type = "password"; passwordToggleIcon.classList.remove("fa-eye-slash"); passwordToggleIcon.classList.add("fa-eye"); } }); passwordInput.addEventListener("focus", function() { passwordToggleIcon.style.display = "block"; }); passwordInput.addEventListener("blur", function() { passwordToggleIcon.style.display = "none"; }); ``` 这段代码中,我们首先在密码输入框的外层包裹了一个 div 容器,并在其中添加了一个小眼睛图标。然后,我们给小眼睛图标添加了点击事件监听器,当点击小眼睛图标时,会切换密码输入框的 type 属性,并改变小眼睛图标的样式以反映当前的密码显示状态。 为了在合适的时机显示/隐藏小眼睛图标,我们给密码输入框添加了 focus 和 blur 事件监听器。当密码输入框获取焦点时,我们显示小眼睛图标;当密码输入框失去焦点时,我们隐藏小眼睛图标。 需要注意的是,这段代码中用到了 Font Awesome 图标库中的图标,需要在 HTML 中引入对应的 CSS 文件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值