css - 遇到一个样式问题
本文介绍了css - 遇到一个样式问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
就是想做个小验证 如果没有输入内容就在失去焦点时把边框变成红色 我是这么设置的样式1px solid red 代码运行没问题 但是鼠标离开输入框时边框不但变成了红色还有一个细微的变化就是失去焦点的这一行和上一行的间距缩小了那么一点 就是margin-top发生了变化 我把失去焦点的边框变成2px solid red间距就没变化 但是改成2px就太难看了 这个应该怎么办呢?
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>
</title>
<style type="text/css">
div input{display: inline-block;width: 200px;height: 20px;}
div .span1{display: inline-block;width: 90px;height: 20px;text-align: left;margin-top: 20px;}
div #sp1,#sp2,#sp3,#sp4{display: inline-block;width: 160px;height: 20px;}
</style>
<script src="ajax"></script>
<script type="text/javascript">
window.onload=function(){
var oInput=document.getElementsByTagName('input');
var oIpt
for(i=0;i<oInput.length;i++){
oInput[i].onblur=function(){
if(this.value==''){
this.style.border='1px solid red';
}
}
}
}
</script>
</head>
<body>
<div>
<form>
<span class="span1">用 户 名:</span><input type="text" id="ipt1"><span id="sp1"></span><br />
<span class="span1">密 码:</span><input type="password" id="ipt2"><span id="sp2"></span><br />
<span class="span1">确认密码:</span><input type="password" id="ipt3"><span id="sp3"></span><br />
<span class="span1">邮 箱:</span><input type="text" id="ipt4"><span id="sp4"></span><br />
<button type="submit">提交</button>
</form>
</div>
</body>
</html>
解决方案
div input{display: inline-block;width: 200px;height: 20px;border:1px solid #ccc}
这篇关于css - 遇到一个样式问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文