效果:
思路:一个div里面含有一个input和一个p。
div用于将整个组件放在中间,放在中间的关键是margin= 100px auto;意思是上下100px,左右平分,这样就实现水平居中。
input用于输入密码,当input失去焦点的时候判断value的长度,如果小于6大于16就改变p的背景,显示错误字样和红色;如果在6-16显示正确字样。
p用于显示图标和文字,图标实际是p的背景图片+no repeat.将p显示在input的右边的关键是将p的diplay属性改为inline-block。
行内元素:宽高无效,margin的上下无效,左右有效,pading的上下左右有效,高度靠行内元素和padding撑起,自动换行。
块及元素:宽高有效,自为一行。
行内块元素:宽高有效,自动换行。
代码:
<!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{
width: 600px;
margin: 100px auto;
}
p{
background: url(./images/mess.png) no-repeat ;
display: inline-block;
font-size: 12px;
color: #999;
padding-left: 20px;
}
input{
}
.wrong{
color: red;
background: url(./images/wrong.png) no-repeat;
}
.right{
color: aquamarine;
background: url(./images/right.png) no-repeat;
}
</style>
</head>
<body>
<div class="box">
<input type="password" class="ipt">
<p class="message">Please input 6~16 password</p>
</div>
<script>
var ipt=document.querySelector('.ipt');
var message=document.querySelector('.message');
ipt.onblur=function(){
if(this.value.length<6 || this.value.length>16){
message.className='p wrong';
message.innerHTML='wrong format,please input 6-16 password'
}else{
message.className='p right';
message.innerHTML='you are right!';
}
}
</script>
</body>
</html>