1. 使用js是的文本框只能输入正数,当输入的不是的时候,文本框变成红色
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html lang="en"> <head> <script type="text/javascript"> function checkDate() { var date=parseInt(document.getElementById(input).value); if(isNaN(date)||date<=0||!(/^d+$/.test(nubmer))){ document.getElementById(input).style.color="red"; return false; } } </script> </head> <body> <input type="input"id="input" onBlur="checkDate()"/> </body> </html>
2. 设计一个div居中,且里面显示当前的时间
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html lang="en"> <head> <script type="text/javascript"> function showLeftTime() { var now=new Date(); var year=now.getYear(); var month=now.getMonth(); var day=now.getDate(); var hours=now.getHours(); var minutes=now.getMinutes(); var seconds=now.getSeconds(); document.all.show.innerHTML=""+year+"年"+month+"月"+day+"日 "+hours+":"+minutes+":"+seconds+""; //一秒刷新一次显示时间 var timeID=setTimeout(showLeftTime,1000); } </script> <style type="text/css"> div { border: 1 solid gray; position: absolute; /*绝对定位*/ top: 50%; /* 距顶部50%*/ left: 50%; /* 距左边50%*/ height: 200px; margin-top: -100px; /*margin-top为height一半的负值*/ width: 400px; margin-left: -200px; /*margin-left为width一半的负值*/ } </style> </head> <body onload="showLeftTime()"> <div align="center" id="show"></div> </body> </html>