备注:样式属性操作
① element.style//行内样式操作,适用于样式较少、功能简单的情况
② element.className //类名样式操作,适合样式较多或工能复杂的情况。另外className会直接更改元素的类名,会覆盖原先的类名,若要保留则可以多类名选择器
JS里面修改style样式操作,产生的是行内样式,css权重低
一、密码点击显示
输入密码:
点击图片显示密码:
代码实现:
<!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 img{
width: 24px;
position: absolute;
top: 9px;
right:2px;
width:24px;
}
</style>
</head>
<body>
<div class="box">
<label for="">
<img src="D:\image\img1.jpg" alt="" id="eye">
</label>
<input type="password" id="pwd">
</div>
<script>
var eye = document.getElementById('eye');
var pwd = document.getElementById('pwd');
var flag = 0;
eye.onclick = function(){
if(flag == 0){
pwd.type ='text';
eye.src = 'D:/image/img2.jpg'
flag = 1;
}else{
pwd.type = 'password';
eye.src = 'D:/image/img1.jpg'
flag = 0;
}
}
//按钮点多次不同状态的情况,用flag记录状态
</script>
</body>
</html>
二、实例——显示和隐藏文本框内容
①无输入内容,显示浅灰色“推荐的内容”;
②输入文字,显示深色的文字
③点击其他位置,颜色变浅灰
④无内容时恢复成浅灰色的“推荐的内容”
<!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>
input{
color: #999;
}
</style>
</head>
<body>
<input type="text" value="推荐的内容">
<script>
var text=document.querySelector('input');
// 获得焦点事件
text.onfocus = function(){
console.log('得到了焦点');
if(this.value === '推荐的内容'){
this.value = '';
}
this.style.color = '#333';
}
text.onblur = function(){
console.log('失去了焦点');
if(this.value === ''){
this.value = '推荐的内容';
}
this.style.color = '#999';
}
</script>
</body>
</html>
三、实例——密码框验证信息
判断事件是表单失去焦点
输入长度在6-18位判断为长度合适,否则长度不符合要求
与前面的显示密码内容结合一起
<!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>密码框验证信息</title>
<style>
div{
width: 600px;
margin: 100px auto;
}
.message{
display: inline-block;
font-size: 12px;
color: #999;
background: url(D:/image/img1.jpg) no-repeat right center;
padding-left: 20px;
}
.wrong{
color: red;
background-image: url(D:/image/img2.jpg);
}
.right{
color: green;
background-image: url(D:/image/img3.jpg);
}
.register img{
width: 24px;
/* position: absolute; */
top: 9px;
right:2px;
width:24px;
}
.register input{
width: 150px;
height: 30px;
border: 1px;
outline: none;
border-bottom:1px solid #ccc;
}
</style>
</head>
<body>
<div class="register">
<input type="password" class="ipt" name="" id="">
<img src="D:\image\img1.jpg" alt="" id="eye">
<p class="message">请输入6-16位密码</p>
</div>
<script>
var ipt = document.querySelector('.ipt');
var message = document.querySelector('.message');
ipt.onblur = function(){
if(this.value.length<6 || this.value.length>18 ){
console.log("错误");
message.className = 'message wrong';
message.innerHTML = '您输入的位数不满足要求';
}
else{
message.className = 'message right';
message.innerHTML = '您输入的密码满足位数要求'
}
}
var eye = document.getElementById('eye');
var message = document.querySelector('.message')
var flag = 0;
eye.onclick = function(){
if(flag == 0){
ipt.type ='text';
eye.src = 'D:/image/img2.jpg'
flag = 1;
}else{
ipt.type = 'password';
eye.src = 'D:/image/img1.jpg'
flag = 0;
}
}
</script>
</body>
</html>