1.自动选中输入框中的文字
在<input>标签中加入οnfοcus=“this.select()”属性即可。
2.鼠标悬停在输入框时改变边框颜色或背景色
方法一,在css中加入input:hover{border:1px solid #f00}即可;
方法二,在<input>标签中加入οnfοcus=“this.select()” οnmοuseοver="this.style.bordercolor='#f00'" οnmοuseοut="this.style.bordercolor=''"即可。
3.点击输入框默认文字消失
方法一,<input type="text" value="请输入用户名" οnfοcus="if(value=='请输入用户名') {value=''}" οnblur="if (value=='') {value='请输入用户名'}">直接在input中定义onfocus和onblur事件,缺点:鼠标一离开就显示提示文字,输入框中用户的输入也会消失。
方法二, 鼠标点击输入框,输入框获得焦点时,提示文字自动消失 鼠标焦点离开输入框,提示文字根据情况显示输入框html代码;<input id="searchKey" type="text" name="q" value="" tips="请输入关键字"/><br />
<script language="javascript" type="text/javascript">
window.onload = function(){
var o=document.getElementByIdx_x("searchKey");
o.setAttribute("valueCache",o.value);
o.onblur = function(){ if(o.value=="")
{ o.valueCache=""; o.value=o.tips;
}
else
o.valueCache=o.value;
}
o.onfocus = function(){
o.value=o.valueCache; //光标始终在文字最后
var e = event.srcElement;
var r =e.createTextRange();
r.moveStart('character',e.value.length);
r.collapse(true);
r.select();
}
o.onblur();
}
<script>
4.输入字时输入框边框闪烁(边框为小方型):
<Script Language="JavaScript">
function borderColor(){
if(self['oText'].style.borderColor=='red'){
self['oText'].style.borderColor = 'yellow';
}else{
self['oText'].style.borderColor = 'red';
}
oTime = setTimeout('borderColor()',400);
}
</Script>
<input type="text" id="oText" style="border:5px dotted red;color:red" οnfοcus="borderColor(this);" οnblur="clearTimeout(oTime);">
5.输入字时输入框边框闪烁(边框为虚线):
<style>
#oText{border:1px dotted #ff0000;ryo:expression(οnfοcus=function light (){with(document.all.oText){style.borderColor=(style.borderColor=="#ffee00"?"#ff0000":"#ffee00");timer=setTimeout(light,500);}},οnblur=function(){this.style.borderColor="#ff0000";clearTimeout(timer)})};
</style>
<input type="text" id="oText">
6.自动横向廷伸的输入框:
<input type="text" style="huerreson:expression(this.width=this.scrollWidth)" value="abcdefghijk">
7.自动向下廷伸的文本框:
<textarea name="content" rows="6" cols="80" onpropertychange="if(this.scrollHeight>80) this.style.posHeight=this.scrollHeight+5">输入几个回车试试</textarea>
8.只有下划线的文本框:
<input style="border:0;border-bottom:1 solid black;background:;">
9.软件序列号式的输入框:
<script for="T" event="onkeyup">
if(value.length==3)document.all[event.srcElement.sourceIndex+1].select();
</script>
<input name="T" size="5" maxlength="3">—<input name="T" size="5" maxlength="3">—<input name="T" size="5" maxlength="3">—<input name="T" size="5" maxlength="3">—<input name="T" size="5" maxlength="3">—<input name="T7" size="5" maxlength="3">
10.软件序列号式的输入框(完整版):
<script for="T" event="onkeyup">if(value.length==maxLength)document.all[event.srcElement.sourceIndex+1].focus();</script>