ie浏览器实现保留记住账号但不会自动填充密码功能

项目中业务提出了这么一个需求:保留ie记住账号的功能(双击账号栏会出现记住账号列表,填写账号时会智能提示记住账号中符合的账号。都是ie自带的功能),但是不能自动填充密码。最终解决方法丢在了结尾。

百度了下,有大佬总结了不少方案,但是都不能很好地实现:

https://blog.csdn.net/xw505501936/article/details/52129579

提一下上面这篇文章的其中一个方案:

方案一、密码栏添加属性 autocomplete="off",不生效就改为autocomplete= "new-password" ×

博主用这个方案可以解决,但在我的这个项目中两种写法都不能阻止浏览器自动填充密码,下一个。

 
方案二、百度到的一些其它的方案,比如添加一个隐藏的form表单、添加隐藏的密码栏 ×

 <form style="display: none;">
	<input type="password" />
 </form>

或者

<input type="password" name="testPwd" id="testPwd" style="width: 0px;height: 0px;float:left"/>

这些方案都只能做到阻止ie浏览器自动填充密码,但是也会使记住密码功能失效(新账号登录时不会再提示是否记住账号密码),不符合需求要求。

 

方案三、修改readOnly属性,在输入账号时将密码栏readonly设置为true,切换到密码栏时移除readonly属性。

这个方案能很接近很接近需求。但是这种效果有个不大不小的毛病,就是密码栏在移除readonly属性后依然是不可输入状态,需要鼠标再点击一次密码栏。原因至今不懂,但我可以尝试用代码模拟再点击一次密码栏的效果。

方案3.1、在密码栏的点击事件中,执行一遍focus方法、click方法。×

都没能解决。

 

方案3.2、新增一个隐藏的输入栏,点击密码栏时先把焦点跳转到隐藏的输入栏,隐藏的输入栏的onfocus事件中再把焦点跳转回密码栏

成功实现功能!!

demo代码如下

<!doctype html>
<html> 
<head>
<script type="text/javascript">
var testFlag=true;
function testfun(){
	testFlag=true;
	document.getElementById("loginPwd").setAttribute('readonly','true');
}

function testfunc2(){	
	document.getElementById("loginPwd").removeAttribute('readonly');
	if(testFlag){
		document.getElementById("loginPwd2").focus();
	}
}

function testfunc3(){
	if(testFlag){
		testFlag=false;
		document.getElementById("loginPwd").focus();
	}
}
</script>
<meta charset="utf-8">
<title>无标题文档</title>
</head> 
<body>
<form>
<input type="text" id="loginName" name="loginName" onfocus="testfun()"/>
<input type="password" id="loginPwd" name="loginPwd" onfocus="testfunc2()" />
<input type="text" id="loginPwd2" name="loginPwd2" onfocus="testfunc3()" style="width:0px;height:0px;opacity:0" tabindex="-1" />
</form>
</body> 
</html>

需要注意隐藏输入栏别放在账号输入框与密码输入框之间,会导致触发不了记住密码功能。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现Vue登录页面记住密码功能,可以使用localStorage存储已登录过的账号密码,然后在登录页面中获取localStorage中的数据,进行判断和展示。 具体实现步骤如下: 1. 在登录页面中添加记住密码的选项,可以使用checkbox实现,例如: ``` <label> <input type="checkbox" v-model="remember"> 记住密码 </label> ``` 2. 在登录页面的created生命周期中,获取localStorage中存储的已登录过的账号密码。如果存在,则将其赋值给登录表单中的用户名密码字段。例如: ``` created() { const users = JSON.parse(localStorage.getItem('users')) || [] const lastUser = users[users.length - 1] if (lastUser && lastUser.remember) { this.username = lastUser.username this.password = lastUser.password this.remember = true } } ``` 3. 在登录成功后,判断是否选择了记住密码选项。如果选择了,则将当前登录的账号密码存储到localStorage中。例如: ``` login() { // 登录逻辑 if (this.remember) { const user = { username: this.username, password: this.password, remember: true } const users = JSON.parse(localStorage.getItem('users')) || [] const index = users.findIndex(u => u.username === this.username) if (index === -1) { users.push(user) } else { users.splice(index, 1, user) } localStorage.setItem('users', JSON.stringify(users)) } } ``` 4. 如果需要自动输入密码,可以在登录表单的mounted生命周期中,监听用户名字段的变化。如果输入的用户名在localStorage中存在对应的密码,则自动填充密码字段。例如: ``` mounted() { this.$watch('username', (newVal) => { const users = JSON.parse(localStorage.getItem('users')) || [] const user = users.find(u => u.username === newVal && u.remember) if (user) { this.password = user.password this.remember = true } else { this.password = '' this.remember = false } }) } ``` 这样就实现了Vue登录页面记住密码自动填充密码功能。需要注意的是,由于localStorage中存储的是明文密码,存在一定的安全风险,建议在实际项目中使用加密存储。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值