JS实现日期输入格式提示功能_日期时间特效

模板酷站
  •       3/5
  •       1
  •       2
  •       3
  •       4
  •       5
查看演示效果

      织梦DedeCMS视频教程买空间 租服务器 选网硕互联! 无忧站长工具,百度权重一键全查!

      用JS配合CSS实现的日期输入格式提示功能,当用户点击文本框准备输入的时候,自动出现日期格式的提示,以避免用户不知道输入什么样的日期格式而使程序出错,本代码段中一共有两种样式的时期提示,至于你想用哪一种你自己眩

      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" mrc="text/html; charset=gb2312" /> <TITLE>日期输入提示</TITLE> <script type="text/javascript"> var data_help = function(inp, format){ this.inp = document.getElementById(inp); this.format = format; var div = document.createElement('div'); div.setAttribute('style', 'color:#f33; text-indent:3px; position:absolute; visibility:hidden'); div.style.cssText = 'color:#f33;border-top:0; text-indent:3px; position:absolute; visibility:hidden'; div.innerHTML = this.format; this.div = div; document.body.appendChild(div); this.load(); var oThis = this; this.inp.onfocus = function(){ oThis.inp_v.call(oThis); } this.inp.onblur = function(){ oThis.inp_h.call(oThis); } this.inp.onkeyup = function(){ oThis.inp_chk.call(oThis); } } data_help.prototype={ load: function(){ var inp = this.inp; var inpW = inp.offsetWidth, inpH = inp.offsetHeight; var left = 0, top = 0; while(inp != null){ left += inp.offsetLeft; top += inp.offsetTop; inp = inp.offsetParent; } this.div.style.height = '21px'; this.div.style.width = inpW-2 + 'px'; this.div.style.left = left + 'px'; this.div.style.top = inpH+top + 'px'; }, inp_v: function(){ this.div.style.visibility = 'visible'; }, inp_h: function(){ this.div.style.visibility = 'hidden'; }, inp_chk: function(){ var p = this.inp.value.replace(/\s/g,''); var w = this.format; var n=0; for(var i=0; i<p.length; i++){ if(/[^a-zA-Z]/.test(w.charAt(i))){ if(p.charAt(i)!=w.charAt(i)){ break; } } n++; } this.div.innerHTML= '<span style="font-weight:bold; color:green">'+w.slice(0,n)+'</span>'+w.slice(n,w.length); } } window.onload = function(){ new data_help('birthday', 'yyyy/mm/dd'); new data_help('birthday2', 'yyyy-mm-dd'); } </script> <style> fieldset{ width:300298px; padding:22px;} </style> </HEAD> <body> <div> 提示样式A:<input type="text" id="birthday" /><br/><br /> 提示样式B:<input type="text" id="birthday2" /> </div> </body> </html>