文本框内默认提示————请输入用户姓名(字体灰色),要求: (1)当文本框获得焦点时,默认提示消失 (2)当文本框失去焦点时,如果没有输入新内容,那么则显示原来的灰色字体内容; 如果有新内容输入则判断,如果长度小于10,则提示“姓名长度应该大于10个字符”
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<script>
function txtfocus(){
var mytxt=document.getElementById("txt");
if(mytxt.value=="请输入用户名"){
mytxt.value="";
mytxt.style.color="#121212";
}
}
function txtonblur(){
var mytxt=document.getElementById("txt");
if(mytxt.value==""){
mytxt.value=mytxt.defaultValue;
mytxt.style.color="#c7d1cc";
}else if(mytxt.value.length<10){
alert("姓名长度应该大于10个字符");
}
}
</script>
</head>
<body>
<input id="txt" type="text"value="请输入用户名" style="color:#c7d1cc" onfocus="txtfocus()"
onBlur="txtonblur()"/>
<input type="text" id="mytxt" />
</body>
</html>

浙公网安备 33010602011771号