从网上找的代码,自己封装了一下(前提:引用jQuery库)
HTML:
1 <div class="box"> 2 <div class="ipt1" ><input type="text" id="user" value="name" ></div> 3 <div class="ipt2" ><input type="text" id="tel" value="phone"></div> 4 <input type="submit" class="submit" value="确 认"> 5 </div>
javascript:
1 function InputStyleChange(id,p,c1,c2){ 2 //函数 3 var placeholder = p; 4 var inputname = id; 5 inputname.onfocus = function(){ 6 if ( this.value == placeholder ) { 7 this.value = ‘‘; 8 this.style.color = c1; 9 } 10 }; 11 inputname.onblur = function(){ 12 if (!this.value) { 13 this.value = placeholder; 14 this.style.color = c2; 15 } 16 }; 17 18 if (inputname.value == placeholder) { 19 inputname.style.color = c2; 20 } 21 } 22 23 $(function){ 24 InputStyleChange($(‘#tel‘)[0],‘phone‘,‘#323232‘,‘#b4b4b4‘); 25 //调用函数 26 //修改默认显示文字需要与Html表单默认文字对应 27 })
时间: 2024-11-05 20:45:03