我已经实现了占位符,就像twitter和google一样,但出于某些原因,我想改进这段代码。当有人检查元素时,我希望只允许获取输入元素
以下是代码
<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比input.....is大,这样可以使输入和占位符在宽度和高度上保持一致
发布于 2012-07-25 14:33:47
对于你的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;
}发布于 2012-07-25 14:21:32
嘿,现在习惯了这个,就像直播这个
<input type="text" onkeypress="return submitenter(this,event)" id="" name="" onfocus="this.value = ''" onblur="if(this.value == '') this.value='Enter Password'" value="Enter Password">还是这一个
<input type="text" placeholder="Enter Password">https://stackoverflow.com/questions/11643746
复制相似问题