加入收藏 | 设为首页 | 会员中心 | 我要投稿 银川站长网 (https://www.0951zz.com/)- 云通信、基础存储、云上网络、机器学习、视觉智能!
当前位置: 首页 > 综合聚焦 > 编程要点 > 语言 > 正文

基于JS怎样实现密码框的日常功能

发布时间:2023-08-04 12:53:17 所属栏目:语言 来源:
导读:这篇文章将为大家详细讲解有关“基于JS怎样实现密码框的常见功能”的知识,下文有详细的介绍,小编觉得挺实用的,对大家学习或工作或许有帮助,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所

这篇文章将为大家详细讲解有关“基于JS怎样实现密码框的常见功能”的知识,下文有详细的介绍,小编觉得挺实用的,对大家学习或工作或许有帮助,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

学前端最基本的登录页面肯定要会写,那登录页面里面的密码框的功能设计就需要好好打磨,主要功能有显示密码明文,密码检测信息提示等等,那么本篇博客将写写这些功能结合js怎么做,很简单,看一下就会了。

显示隐藏密码明文

1.用到的图片素材

2.代码

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta http-equiv="X-UA-Compatible" content="IE=edge">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Document</title>

<style>

.box{

position: relative;

width: 400px;

border-bottom: 1px solid #ccc;

margin: 100px auto;

}

.box input{

width: 370px;

height: 30px;

border: 0;

outline: none;

}

.box .image{

position: absolute;

top:2px;

right: 2px;

border: 1px solid #ccc;

background-color: #ccccccba;

border-radius: 50%;

overflow: hidden;

}

.box img{

vertical-align: bottom; /*父盒子是由img撑开的,所以要去除下面的间隙*/

width: 22px;

}

</style>

</head>

<body>

<div class="box">

<div class="image">

<img src="close.png" alt="" id="img">

</div>

<input type="password" id="pwd">

</div>

<script>

var img = document.getElementById('img');

var pwd = document.getElementById('pwd');

var flag = 0;

img.onclick = function(){

if(flag == 0){

pwd.type = 'text';

this.src='open.png'; // this指向事件函数的调用者,即img

flag = 1;

}else{

pwd.type = 'password';

this.src='close.png';

flag = 0;

}

}

</script>

</body>

</html>

密码框验证信息

1.用到的图片素材

2.代码

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta http-equiv="X-UA-Compatible" content="IE=edge">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Document</title>

<style>

.register{

width: 600px;

margin: 100px auto;

}

.ipt{

width: 250px;

border: 1px solid #999;

outline: none;

}

.message{

display: inline-block;

height: 20px;

font-size: 12px;

color: #999;

background: url(mess.png) no-repeat left center;

padding-left: 30px;

}

.wrong{

color: red;

background: url(wrong.png) no-repeat left center;

}

.right{

color: green;

background: url(right.png) no-repeat left center;

}

</style>

</head>

<body>

<div class="register">

<input type="password" class="ipt">

<p class="message">请输入6~16位密码</p>

</div>

<script>

var ipt = document.querySelector('.ipt');

var msg = document.querySelector('.message');

ipt.onfocus = function(){

this.style.borderColor = 'skyblue'

}

ipt.onblur = function(){

this.style.borderColor = '#999';

if(this.value.length<6 || this.value.length>16){

msg.className = 'message wrong';

msg.innerHTML = '你输入的位数不符合要求6~16位!';

}else{

msg.className = 'message right';

msg.innerHTML = '你输入的正确!';

}

}

</script>

</body>

</html>

以上就是关于“基于JS怎样实现密码框的常见功能”的介绍了,感谢各位的阅读,如果大家想要了解更多相关的内容,欢迎关注群英网络,小编每天都会为大家更新不同的知识。

(编辑:银川站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章