input文本框的常用操作

来源:互联网 发布:未来网络发展趋势 编辑:程序博客网 时间:2024/05/18 20:07

1、选中去除文本框文字,离开后显示原有文字:

[html] view plain copy
  1. <span style="font-size:14px;"><input name="key" type="text" id="key" value="邮箱" size="30"    
  2. onmouseover=this.focus();this.select();   
  3. onclick="if(value==defaultValue){value='';this.style.color='#000'}"     
  4. onBlur="if(!value){value=defaultValue;this.style.color='#999'}" style="color:#999" /></span>  


2、复选框选中后对文本框可操作:

[html] view plain copy
  1. <span style="font-size:14px;"><input type="checkbox" name="tpbox" value="1"   
  2. onclick="if(this.checked) {document.getElementById('txtNo').disabled=false}else{  
  3. document.getElementById('txtNo').disabled=true}">  
  4. <input type="text" id="txtNo" name="txtNo" size="20" value="选中前面的选项方可编辑" disabled></span>  

3、点击链接后方可编辑:

[html] view plain copy
  1. <span style="font-size:14px;"><a href="#" onclick="document.getElementById('username').readOnly=false;alert('你好,欢迎使用!')">  
  2. 先点击我哦!</a>  
  3. <input id="username" value="--请输入--" size="30" readOnly></span>  

4、输入框从中间输入:

[html] view plain copy
  1. <span style="font-size:14px;"><input type="text" name="mid" style="text-align:center;"></span>  

5、聚焦时,输入框改变变色:

[html] view plain copy
  1. <span style="font-size:14px;"><input type="text" size="20" style="background-color:#FFFFFF"  
  2.         onfocus="style.backgroundColor='#FFFF00'"  
  3.         onblur="style.backgroundColor='#FFFFFF'"></span>  

6、输入框只能输入数字(用的是正则表达式):

[html] view plain copy
  1. <span style="font-size:14px;"><input onkeyup="value=value.replace(/[^\d]/g,'') "  
  2. onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))"></span>  

7、输入框只能输入中文(用的是正则表达式):

[html] view plain copy
  1. <input onkeyup="value=value.replace(/[ -~]/g,'')"  
  2. onkeydown="if(event.keyCode==13)event.keyCode=9">  

8、只能输入英文和数字(用的是正则表达式):

[html] view plain copy
  1. <input onkeyup="value=value.replace(/[\W]/g,'') "      
  2. onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))"  
  3. onkeydown="if(event.keyCode==13)event.keyCode=9">  

9、只读状态,输入框不能编辑,但表单可以获得输入框内的值,框内字体颜色为黑体:
[html] view plain copy
  1. <input type="text" value="afreon" onclick="alert('不能编辑!');" onfocus="this.blur()" />  
  2. <input type="text" value="afreon" onclick="alert(this.value);" readonly />  
  3. <input value="不可修改"  readonly"true" type="text"/>  

10、禁用input,输入框不能编辑,并且表单不能获得输入框内的值,字体颜色为灰体:

[html] view plain copy
  1. <input value="不可修改也不能获取值" disabled="disabled"  type="text"/>  

11、输入框禁止输入法(不能复制黏贴):

[html] view plain copy
  1. <input onpaste="return false" style="ime-mode:disabled"><pre></pre>  
  2. <pre></pre>  
  3. <link rel="stylesheet" href="http://static.blog.csdn.net/public/res-min/markdown_views.css?v=2.0">  
  4.    
原创粉丝点击