做一个练习想实现世纪佳缘网登录框的一个小效果
点击鼠标时,边框为粉色,字体为浅色
移除鼠标时,边框为浅色,字体为深色
修改元素的时候边框效果一直无法实现,是因为缺少了
outline:none; 给登录框(ipt)css样式增加即可
div {
width: 600px;
margin: 100px auto;
}
.ipt {
width: 120px;
height: 23px;
padding: 0 4px 0 10px;
outline:none;
}
.hd {
border: 1px solid #ffd6db;
color: #888;
}
.sq {
border: 1px #d9d9d9 solid;
color: #333;
}
</style>
<body>
<div>
<input type="text" class="ipt" value="邮箱/ID/手机号">
</div>
<script>
// 1.获取元素
var ipt = document.querySelector('.ipt');
// 2.注册事件,获得焦点onfocus
ipt.onfocus = function () {
if(this.value === '邮箱/ID/手机号'){
this.style.color = '#888'
this.value = '';
}
this.className = ' ipt hd'//双类名
}
// onblur 失去焦点
ipt.onblur = function () {
if(this.value === ''){
this.value = '邮箱/ID/手机号';
}
this.className = ' ipt sq';//双类名
}
</script>
</body>