初学js遇到的问题
直接上代码:
<html>
<head>
<script type="text/javascript">
function checkMail(){
var email = document.getElementById("email");
var patrn=/((\w)|[-]|[.])+@(((\w)|[-])+[.])+[a-z]{2,4}$/;
var checkMail = document.getElementById("checkMail");
if(!patrn.exec(email.value)){
alert("进入错误格式");
checkMail.style.color="red";
checkMail.innerHTML="邮箱格式输入有误!";
return false;
}else{
alert("进入正确格式");
checkMail.style.color="green";
checkMail.innerHTML="√ 邮箱输入正确!";
return true;
}
}
function clearMail(){
var checkMail = document.getElementById("checkMail");
checkMail.style.color="black";
checkMail.innerHTML="请输入正确的邮箱地址";
}
</script>
</head>
<body>
<form name="register_form" action="" method="post">
<tr>
<td>电子邮件:</td>
<td><input type="text" id="email" name="email" onblur="checkMail()" onfocus="clearMail()" /></td>
<td id="checkMail"></td>
</tr>
</form>
</body>
</html>
我的目的是:在text里输入邮箱后,失去焦点后,执行 onblur="checkMail()" alert()是出来了,但是后面的没出来。
重新输入:后面也是一样:alert()是出来了,黑体的"请输入正确的邮箱地址";没有
------解决方案--------------------补全 table
<html>
<head>
<script type="text/javascript">
function checkMail(){
var email = document.getElementById("email");
var patrn=/((\w)|[-]|[.])+@(((\w)|[-])+[.])+[a-z]{2,4}$/;
var checkMail = document.getElementById("checkMail");
if(!patrn.exec(email.value)){
alert("进入错误格式");
checkMail.style.color="red";
checkMail.innerHTML="邮箱格式输入有误!";
return false;
}else{
alert("进入正确格式");
checkMail.style.color="green";
checkMail.innerHTML="√ 邮箱输入正确!";
return true;
}
}
function clearMail(){
var checkMail = document.getElementById("checkMail");
checkMail.style.color="black";
checkMail.innerHTML="请输入正确的邮箱地址";
}
</script>
</head>
<body>
<form name="register_form" action="" method="post">
<table>
<tr>
<td>电子邮件:</td>
<td><input type="text" id="email" name="email" onblur="checkMail()" onfocus="clearMail()" /></td>
<td id="checkMail"></td>
</tr>
</table>
</form>
</body>
</html>
------解决方案--------------------补全 table