我已经像推特和谷歌一样实现了占位符 但出于某种原因我想改进这段代码。 当有人检查元素时,我只想允许获取输入元素
这是代码
<style type="text/css">
input.txtl{padding: 4px 4px;width: 100%;border: 0px}
div.txtlo{width: 200px;position: relative;border: 1px solid #000099;}
div.placer{position: absolute;z-index: 5;top: 0px;left: 0px;background: transparent;width: 100%;color: #cccccc;white-space: nowrap;padding: 2px;padding-left: 4px}
div.hints{background: transparent;margin: 3px}
</style>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<script type="text/javascript">
function perform(val,hid){
val=val.trim();
if(val==""){
$("#"+hid).show();
}else{
$("#"+hid).hide();
}
}
</script>
<div class="txtlo">
<input type="text" class="txtl" id="us" onkeyup="perform(this.value,'plcus')" />
<div class="placer" onclick="$('#us').focus()" id="plcus">Enter Password</div>
</div>
现在,当我使用 firefox 的默认检查器检查它时,我得到了这个。
正如您在这张图片中看到的那样,显示了蓝色边框,这意味着我的占位符 div 大于输入......是否有某种方式可以使输入和占位符的宽度和高度相同
最佳答案
对于你的 css 问题试试这个。根据您的需要进行调整。
演示: http://jsfiddle.net/elclanrs/bCqQp/
.txtlo, .placer, .txtl {
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
box-sizing: border-box;
}
.txtlo {
position: relative;
width: 200px;
height: 30px;
line-height: 30px;
padding: 0 1em;
border: 1px solid black;
}
.placer {
position: absolute;
top: 0;
left: 0;
width: 100%;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
padding: 0 1em;
}
.txtl {
display: block;
width: 100%;
border: none;
}
关于jquery - 改进占位符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11643746/